Understanding ASCII in Embedded development

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
Microcontroller only understand binary number system. Binary numbers are very difficult to read for humans. It is common for humans to use the ASCII code to display things.

http://www.asciitable.com/

Binary to ASCII or Hex to ASCII conversion is one of the most confusing concepts for me

I see a one conversion method in which value is sent in two pieces but I don't understand why this is done ?

Can you @MrChips help me if possible
 

Papabravo

Joined Feb 24, 2006
21,225
Any string of binary digits can have multiple representations. All of the following representations refer to the exact same binary string.
  1. Binary string , 8 bits --> 0100 0001
  2. The letter 'A' in the ASCII character set
  3. The octal number --> 101
  4. The decimal number --> 65
  5. The hexadecimal number --> 41
Notice that the two hexadecimal digits can be determined by breaking the 8 bit binary string into two 4-bit pieces, and converting those 4 bit pieces to their hexadecimal equivalent.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
Notice that the two hexadecimal digits can be determined by breaking the 8 bit binary string into two 4-bit pieces, and converting those 4 bit pieces to their hexadecimal equivalent.
That's point I don't understand. How to convert those 4 bit pieces to their hexadecimal equivalent.

  1. Binary string , 8 bits ---------> 0100 0001
  2. The hexadecimal number ---> 41
  3. Higher Nibble ---------------> 0100
  4. Lower Nibble ----------------> 0001

ASCII character 4 ---> 0x34
ASCII character 1----> 0x31
 

MrChips

Joined Oct 2, 2009
30,807
I am glad that you asked. If you can understand and then master this concept you would have made a quantum leap into understanding how computers work.

You are correct. Computers operate on binary numbers, nothing else.
There is no such thing as Hex to ASCII conversion. Hex is already ASCII.

Start off with binary to hex conversion first. This is easier to understand than binary to decimal conversion.
The value 65 is stored as 0100 0001 if stored as a byte.
Split the 8 bits into two 4-bit nybbles.
Output 0100 as "4".
Output 0001 as "1".
Think of the "4" and "1" as icons, i.e. pictures that appear on a computer screen. Every little icon has to be given an identifying code. This code is ASCII. You need to know how to find the right code given sixteen values 0-15.
 

MrChips

Joined Oct 2, 2009
30,807
That's point I don't understand. How to convert those 4 bit pieces to their hexadecimal equivalent.
There is no such thing as hexadecimal equivalent.
I state this fact in order to get you out of this state of mind.

0100 on a 4-bit computer is just that, four bits. There is no such thing as hexadecimal notation on a computer.
Hexadecimal notation is a series of pictures in the human's head.

0000 = "0"
0001 = "1"
0010 = "2"
0011 = "3"
:
:
1001 = "9"
1010 = "A"
:
:
1111 = "F"

Each "0" to "F" are little pictures or icons. We could just as well substitute these with Japanese numerals.

Edit:
We could output
0100 as
0101 as

but then we would run into a problem with 1010 because as far as I know there is no "A" equivalent in Japanese.
 

Papabravo

Joined Feb 24, 2006
21,225
There is no such thing as hexadecimal equivalent.
I state this fact in order to get you out of this state of mind.

0100 on a 4-bit computer is just that, four bits. There is no such thing as hexadecimal notation on a computer.
Hexadecimal notation is a series of pictures in the human's head.

0000 = "0"
0001 = "1"
0010 = "2"
0011 = "3"
:
:
1001 = "9"
1010 = "A"
:
:
1111 = "F"

Each "0" to "F" are little pictures or icons. We could just as well substitute these with Japanese numerals.
Japanese numerals? When I was there, they all used Arabic numerals 0 to 9, just like we do. Was there an ancient set of Japanese numerals?
 

MrChips

Joined Oct 2, 2009
30,807
We could output
0100 as
0101 as

but then we would run into a problem with 1010 because as far as I know there is no "A" equivalent in Japanese.
 

Deleted member 115935

Joined Dec 31, 1969
0
Japanese numerals? When I was there, they all used Arabic numerals 0 to 9, just like we do. Was there an ancient set of Japanese numerals?
The poster is evidently starting
agree with you post,
but , it could confuse the OP.
 

MrChips

Joined Oct 2, 2009
30,807
I teach ASCII using flash cards.

Someone calls out a value and I show a card with a picture on it.
Whenever the computer receives a 7-bit code (for 7-bit ASCII) the computer draws a corresponding picture on the screen.

For example, if you ask for 38, you will see "&".
 

Papabravo

Joined Feb 24, 2006
21,225
We could output
0100 as
0101 as

but then we would run into a problem with 1010 because as far as I know there is no "A" equivalent in Japanese.
Kata Kana has symbols for each of the 5 vowels. I suppose they could one of the remaining consonant-vowel combination letters. Maybe something like:
ah (A) bah (BA) kee (KE) dah(DA) eeh(E) for the hex digits.

One of my mad friends from long ago suggested we use
Ann, Betty, Can, Dot, & Frost as the names of the hex digits. So 0xFD would be spoken as Frosty-Dot, and 0xBC would be Betty-Can, and 0xCD would be Candy-Dot
 

Papabravo

Joined Feb 24, 2006
21,225
The poster is evidently starting
agree with you post,
but , it could confuse the OP.
Well Japan is an ancient society, and they may have had a numbering system that predated the 10th century CE (when Arabic Numerals were introduced in Europe). I'm not aware of one, but it is possible.
EDIT: Yup. There it is on Wikipedia for the asking. I did not connect the post by @MrChips to the existence of such a complete system.
 
Last edited:

Papabravo

Joined Feb 24, 2006
21,225

MrChips

Joined Oct 2, 2009
30,807
I am still waiting for the TS to respond then I will show how there is nothing cryptic about ASCII.
ASCII is simply a look-up table. Binary to ASCII conversion can be implemented using an array.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
I am still waiting for the TS to respond then I will show how there is nothing cryptic about ASCII.
I'm really very confused. The description of what I want to understand is as follow

For Example, There is temperature sensor which is connected with microcontroller. Sensor read the room temperature and transmit 8 bit binary data to the microcontroller. It's not so easy to read binary temperature reading on display. so need to display the temperature reading on the computer screen so that anyone can understand room temperature

EDIT-
I hope main purpose of question is not changing because of example. I'm focusing on original question.
I'm confused by reading some old threads. Each discussion has different suggestion
 
Last edited:

MrChips

Joined Oct 2, 2009
30,807
Think of ASCII as 128 picture cards. We will discuss this concept from the perspective of a display device such as an LCD screen. Ignore input devices and text files for now.

When you observe a number such as 123 on the screen, you are looking at three picture cards [1][2][3] displayed side by side.

Binary to ASCII conversion
Suppose you want to show 0100 on the screen. There are only two picture cards [0] and [1]. Hence we need to select two picture cards from the available set of 128 cards. Let us put the two cards into a look-up table called LUT[ ].

We only need two entries in the LUT.
LUT[0] = 48;
LUT[1] = 49;

When we want to display 0 on the screen we call up card number 48 which is the first entry in the table.
When we want to display 1 on the screen we call up card number 49 which is the second entry in the table.

Binary-coded-decimal (BCD) to ASCII conversion
Now we need ten display cards to show each one of 0123456789.
We need ten entries in the LUT.
LUT[0] = 48;
LUT[1] = 49;
LUT[2] = 50;
LUT[3] = 51;
LUT[4] = 52;
LUT[5] = 53;
LUT[6] = 54;
LUT[7] = 55;
LUT[8] = 56;
LUT[9] = 57;

4-bit binary to ASCII conversion
Now we need sixteen display cards to show each one of 0123456789ABCDEF.
We need sixteen entries in the LUT.
LUT[0] = 48;
LUT[1] = 49;
LUT[2] = 50;
LUT[3] = 51;
LUT[4] = 52;
LUT[5] = 53;
LUT[6] = 54;
LUT[7] = 55;
LUT[8] = 56;
LUT[9] = 57;
LUT[10] = 65;
LUT[11] = 66;
LUT[12] = 67;
LUT[13] = 68;
LUT[14] = 69;
LUT[15] = 70;

Do you understand all of this to this point?
 

BobaMosfet

Joined Jul 1, 2009
2,113
Microcontroller only understand binary number system. Binary numbers are very difficult to read for humans. It is common for humans to use the ASCII code to display things.

http://www.asciitable.com/

Binary to ASCII or Hex to ASCII conversion is one of the most confusing concepts for me

I see a one conversion method in which value is sent in two pieces but I don't understand why this is done ?

Can you @MrChips help me if possible
ASCII (American Standard Code for Information Interchange) is not a numbering system- it's just an arbitrary assignment, a decimal index into a character table.

1623352328580.png

Base conversion made easy.
 

BobTPH

Joined Jun 5, 2013
8,964
Lets say you have an eight bit binary value that represents the number 123 in a variable called temperature.

And we want to display it as a string of characters, '1' then '2' then '3'.

We can get the binary value or the first digit by using temperature / 100, which will be 1.

Now we need to convert that to the ascii character '1' and pass that value to function called put that puts it out on a display.

So one way to do it would be with a bunch of if statements:

Code:
    digit = temperature / 100;
    if (digit = 0) put('0');
    else if (digit == 1) put('1');
    else if (digit == 2) put('2');
...
    else if (digit == '9') put('9');
Or, if we are more clever, we can notice that the decimal values for the ascii character '0' through '9' are hex 30 through hex 39 or decimal 48 through 57. Then we can take our variable digit and add 48 to it and we get the right ascii character!

So now, instead of all the if statements, we can write:
Code:
    digit = temperature / 100;
    put(digit + 48);
But, in C, the quoted characters can be treated as numbers as well, so we change it to:

Code:
    put(digit + '0');
Now, how about the second digit of our temperature, which is 123?

We can get that by dividing by 10 to get 12, then taking the remainder when dividing that by 10, which is 2.

In C remainder is the % operator, so:

Code:
    digit = temperature / 10 % 10;
    put(digit);
Getting the 3rd digit, the 3 is left as an exercise for the reader.

Bob
 

MrChips

Joined Oct 2, 2009
30,807
Continuing with my lesson:

Binary-coded-decimal (BCD) to ASCII conversion
Now we need ten display cards to show each one of 0123456789.
We need ten entries in the LUT.
LUT[0] = 48;
LUT[1] = 49;
LUT[2] = 50;
LUT[3] = 51;
LUT[4] = 52;
LUT[5] = 53;
LUT[6] = 54;
LUT[7] = 55;
LUT[8] = 56;
LUT[9] = 57;

You will notice that there is a simple mathematical relationship between the index of the array and the stored value.
Whatever the BCD value, we simply add 48 to arrive at the stored value.

We can write this simply as:

display_code = BCD + 48;

This works just as well for the two binary values of 0 and 1.
display_code = binary + 48;

This does not work for 4-bit binary values to hexadecimal representation.
 

MrChips

Joined Oct 2, 2009
30,807
As I stated above,

display_code = BCD + 48;

works for binary and BCD but not for hexadecimal representation.

Where did I get the 48?
48 happens to be the index of '0' in the ASCII table, i.e. it is the ASCII code for '0'.

You should avoid using literal constants in your code. Your code will break if they ever change the ASCII codes (which is very unlikely to happen). Instead, you should use:

display_code = BCD + '0';

which will work regardless of whatever translation table is used by the compiler.
 
Top