0

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
eran gal
  • 7
  • 3

4 Answers4

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.

Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
0

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

dbl4k
  • 71
  • 6
  • thanks for your quick answer! so you mean the string "97" is represented by the binary number: 01110010110111 (just wrote the binary rep. of 57 to the left of binary rep. of 55) but then, what about the integer 7351. in binary is represented by the same 01110010110111. how does the computer know which of them i mean? – eran gal Jan 28 '18 at 11:12
  • The thing to remember is that when you're storing a numbers, or any other characters in a string, the string is just a series of characters. A string of "7351" would be represented by decimal ascii codes of 55 51 53 49. try this link out [unit-conversion/ascii](http://www.unit-conversion.info/texttools/ascii/) – dbl4k Jan 28 '18 at 11:14
  • @erangal Computer doesn’t care. You tell it what to do. Computer only handles numbers and does whatever you want with them. – Sami Kuhmonen Jan 28 '18 at 11:18
  • @dbl4k so what about when storing 97 as integer? the computer store it as 1100001 but with a type of integer? – eran gal Jan 28 '18 at 11:27
  • Storing as an integer type works differently to strings, because integers aren't stored as a series of characters. The value of 97 wouldn't be represented by ascii, but exactly as you said, the whole value can be represented by the binary value of 1100001. – dbl4k Jan 28 '18 at 11:33
0

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]);
Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
0

The ASCII decimal values for uppercase characters A through Z range sequentially from 65 to 90. To learn more details and see a full ASCII code chart for all letters, digits, and symbols both upper and lowercase - I highly recommend checking out this helpful article ascii value a to z.

It provides a complete ASCII reference chart and explains the history and use of ASCII character encoding in computing and programming. Knowing the precise ASCII decimal values for A-Z and a-z is useful for processing text in many technical applications.

Hiten
  • 1