ASCII table and character presentation

假装没事ソ 提交于 2019-12-13 05:07:55

问题


We learn in class about ASCII table, and that every character from 128 characters has a unique number from 0-128 representing it. For example "a" is 97 (in binary 97 is 1100001). "%" is 37 (and in binary 37 is 0100101). (I understand that for a fixed length of 7 we should allow the binary number start with 0)

If 97 is representing "a", then what represents the string "97"? What represents the integer 97?


回答1:


I think your question is based on the notion that, given a representation of an integer, string or other type of value, you can decern the type and the value. You can't.

In most digital computer architectures, data is bits, accessed in contiguous 8-bit bytes. You can take a byte, think of it as a non-negative integer, and represent it in binary, octal, decimal, hexadecimal, …. Binary is used when a bit represents a value by itself. Hexadecimal is preferred for its compactness and easy translation to binary. Decimal is used when the whole byte has some cardinal number value to humans, which makes it the choice for negative integers.

So, given the byte, 97 (decimal), say from a 1-byte file or at a memory address, what's the type and value? The only way to know is through some sort of shared understanding: an agreement, declaration, convention, specification, data map, etc. In other words, communication. Complete communication consists of the data and accompanying or separate metadata that indicates how to interpret the bytes.

97₁₀ = 61₁₆ = 01100001₂ could be:

  • As an 8-bit unsigned integer: 97₁₀
  • As an 8-bit two's complement signed integer: 97₁₀
  • As a UTF-8 code unit: happens to be all the code units for the Unicode codepoint: 'a' (U+0061) 'LATIN SMALL LETTER A'
  • As an ASCII code unit: (all ASCII codepoints take one 8-bit code unit): 'a'
  • As an ISO 8859-1 code unit: (all ISO 8859-1 codepoints take one 8-bit code unit): 'a'
  • Anything at all that can be packed into 8 bits.

So, rephrasing your question as: What's the difference 97 representing "a" and 97 representing the integer 97? The answer is in the metadata, not the bytes.




回答2:


Well, the string "97" comprises of two characters so would require two ascii codes, one for "9" and another for "7".

So the answer is 57 and 55




回答3:


ASCII value is returned only when we use char or typecast any unsigned int value to char. So, as per your question, 97 can be represented by int data type. But if you want 97 as characters than you should write following code:

char c[2], i;
c[0]=57;
c[1]=55;
for (i=0; i<2; i++) printf("%c", c[i]);


来源:https://stackoverflow.com/questions/48485573/ascii-table-and-character-presentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!