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 !
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 !
Thank you in advance
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.
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);
}
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).
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.
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.
Where does the data originate (Your PC’s Serial Monitor, Another PC Application, The Arduino)?
In what form is that data?
Where is the data going?
What form do you want the data in once it is received?
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.
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.