Hi, I am trying to find out how I can convert a string of binary like:
1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100
(Hello World)
Back to a string text with Arduino Code. Any suggestions? Thanks!
Hi, I am trying to find out how I can convert a string of binary like:
1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100
(Hello World)
Back to a string text with Arduino Code. Any suggestions? Thanks!
I'm not sure what you mean by 'string of binary'.
Obviously those groups of eight bits each correspond to an ascii character from the string "Hello World", but you haven't said what these mean in terms of data in memory. Have you got eleven bytes of data containing ascii characters? Do you actually have a twelfth byte containing a null terminator? If that isn't eleven or twelve bytes of data, then what is it? Do you mean that each '1' and '0' in that string is held as an ascii character?
Hi, it's not difficult but you are making it slightly more complicated by having variable-length chars (7 and 6 bits), spaces, and no trailing zeroes. A better string representation would be like:
0100100001100101...00100000
Having variable-length chars means that you need to start from the end of the string and proceed backwards until you find a space, filling the remaining bits with zeroes. Fixed length (8 bit) chars allow you to start from the beginning and not caring for the length.
The logic of the conversion is this: in order to convert an array of chars-representing-bits into a byte containing 8 bits you need to reverse the bit order*. What you need in order to put a bit at a certain position within a byte is a combination of bitwise shift and bitwise or. Here's an example:
char bit_string[] = "01001000"; // a single byte in string representation, visible value 48Hex
char dest_char = 0; // the output, 'H' -- must initialize all bits to 0
for (int source_bit_pos = 7; source_bit_pos >= 0; source_bit_pos--) // start from rightmost position
{
int the_bit = bit_string[source_bit_pos] - '0'; // convert from char representation '0', '1' to 0, 1
dest_char |= the_bit<<(7-source_bit_pos);
}
[edit] *bits are ordered from right to left.
[edit2] in the code, must initialize the bits to 0.
TwopointFourGHz:
Hi, I am trying to find out how I can convert a string of binary like:1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100
(Hello World)
Back to a string text with Arduino Code. Any suggestions? Thanks!
Best way is to keep a copy of the original ASCII and only print the binary.
"1001000 1100101 ..etc...."
Do you want to save the output string or just print it?
If save then do you know ahead of time how many chars it will take?
Assuming that you do and I've had way too much caffeine:
char binaryText[] = "1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100";
char *bT = binaryText;
byte textBuf[ 12 ];
byte *text;
void setup()
{
Serial.begin( 9600 );
Serial.println( "\nBegin" );
text = textBuf;
*text = 0;
while ( *bT )
{
Serial.print( *bT );
if ( *bT != ' ' )
{
(*text) *= 2; // without those parens the compiler makes it *(text *= 2);
if ( *bT == '1' ) (*text)++; // without the parens it becomes *(text++);
}
else
{
text++;
*text = 0;
}
bT++;
}
Serial.println( );
Serial.print((char *) textBuf );
Serial.println( );
for ( byte i = 0; i < 12; i++ )
{
Serial.print( textBuf[ i ], HEX );
Serial.print( ' ' );
}
Serial.println( );
}
void loop()
{
}
It was bang my head on the desk for a while there, which didn't help.
The compiler won of course.
@PeterH
Your code worked great, now I am trying to figure out how to break up the string of binary so that each can fit into the char array.
Here is an example of what I am asking about:
String binary = "1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100";
//How do I break up the string so I get 1001000, then 1100101, etc. so I can put it in a char array like:
char bits[] = "1001000";
//So I can use the code you gave me and completely convert the string 'binary' to "Hello World"
Any suggestions? Thanks!
If you want readability/edit-ability and an error check, delimit your text data.
Otherwise why keep ASCII text binary at all? Just store the bytes.