# Help please Conversion from ascii to binary

Hello there,

How can I convert an ascii code to binary !
I searched in the web, I found how to display it in the serial monitor using this "Serial.println(Byte, BIN)" , but I want to store this binary number in a variable in order to use it after !

ASCII code is binary, no conversion needed. The binary value is already stored in the variable Byte.

Hello there,

How can I convert an ascii code to binary !
I searched in the web, I found how to display it in the serial monitor using this “Serial.println(Byte, BIN)” , but I want to store this binary number in a variable in order to use it after !

The binary representation of a number (10110011) is a string of characters that that probably can be stored like a text variable (hello world).

What are you trying to do exactly? Like mentioned, all code is stored in Binary. BUt if you are say converting a received ASCII string to binary, that is different.

How can I convert an ascii code to binary !

Where is this ASCII coming from? How many digits are there?

A single ASCII digit for the number 4 is the hex value 34. So to store that as a number perform a bitwise logical AND operation with the ASCII and the hex number 0x0F.
That will cope with single digits between 0 and 9.

If the number is in hex format you need a bit more.
This function converts a single char hex digit into a number

``````unsigned long convertFromHex(int ascii){
if(ascii > 0x39) ascii -= 7; // adjust for hex letters upper or lower case
return(ascii & 0xf);
}
``````

This function uses a global char array called token filled with hex ASCII characters and used the above function to convert it into a number

``````unsigned long tokenInVar(){ // returns the token ID as a number
unsigned long number = 0;
for(int i=0; i<8; i++) number = (number << 4) | convertFromHex(int(token[i]));
return(number);
}
``````

DavidOConnor:
ASCII code is binary

ASCII code is HEXA*

zoomkat:
The binary representation of a number (10110011) is a string of characters that that probably can be stored like a text variable (hello world).

Right !

mirith:
converting a received ASCII string to binary

that's exactly what i want to do, converting a char into a string wich contain the 8 digit

Grumpy_Mike:
Where is this ASCII coming from? How many digits are there?

Serial.read() ... (8 bits = 2 Hexa)

Thanks a lot

Serial.read() ... (8 bits = 2 Hexa)

Are you sure because that would have some unprintable characters in it plus ones that are likely to turn off the serial stream. If it was like that, then the number would be simply the result of the Serial.read alone without having to do anything.

I'm still not sure we understand what you want to do. Write out what you want to do, with an example, in plain English.

Yes, please clarify your needs exactly. There are fairly easy ways to do conversions from and to bytes, integers, strings, and chars. The problem is there is a lot of them, so we need to know what you are doing.

1. Where does the data originate (Your PC’s Serial Monitor, Another PC Application, The Arduino)?
2. In what form is that data?
3. Where is the data going?
4. What form do you want the data in once it is received?

DavidOConnor:
ASCII code is binary

ASCII code is HEXA*

It is both. One 8-bit memory location can store an ASCII character. Let's look at the character '6'. Is it ASCII? hexadecimal?binary? decimal? octal?

Answer: It is all of these. It is one value, and all of those are nothing more than different ways of representing the value.

It is:
ASCII: '6'
Binary: 00110110
Decimal: 54
Octal: 066

So you see, we really need to kow exactly what you want to "convert", and what you want to "convert" it to.

I put the quotes around convert, because the real question is; What value do you have, and how do you want to display it?

I am not convinced that the OP has the language skills to understand and answer these questions.

Grumpy_Mike:
I am not convinced that the OP has the language skills to understand and answer these questions.

Come on Grumpy, every one needs a chance to learn the basics of programming.

every one needs a chance to learn the basics of programming.

And that is exactly what we are giving him. Look at what we have told him so far and look at his responses. Now look at the silence. If he can actually articulate what he wants I ( and others ) will be more than happy to help. But his record is not good to date. If it is not lack of language skills then it is great disrespect to us all here trying to help.

Grumpy_Mike:

every one needs a chance to learn the basics of programming.

And that is exactly what we are giving him. Look at what we have told him so far and look at his responses. Now look at the silence. If he can actually articulate what he wants I ( and others ) will be more than happy to help. But his record is not good to date. If it is not lack of language skills then it is great disrespect to us all here trying to help.

Instead of being a catfish (all mouth and no ass) on the problem, just post the code you think you are trying to teach. Just curious if you can actually walk your talk.

just post the code you think you are trying to teach. Just curious if you can actually walk your talk.