hi,
I am a bit confused because of a litte problem in my code. I'm trying to send ASCII codes from one arduino to the other one via infra red. Therefor i need to convert the ASCII code to a binary code to send it the way i want to do it.
i had all this printed to the Serial monitor to try to get used to the BIN formatter,
but i dont get why Massage[0], BIN = 1100001 and Massage[0], BIN / 10 = a (the parts after the '=' are just representing what was printed)
Serial.println(Massage[0]);// =97(ASCII code von 'a')
Serial.println(Massage[0], BIN);// = 1100001
Serial.println(Massage[0], BIN / 1);// = 1100001
Serial.println(Massage[0], BIN / 10);// = a
Serial.println(Massage[0], BIN / 100);// = a
Serial.println(Massage[0], BIN / 1000);// = a
Serial.println(Massage[0], BIN / 1000000);// = a
Serial.println(Massage[0], BIN / B1000000);// = a
Serial.println(Massage[0], BIN / 64);//(64=0b1000000)= a
x = Massage[0], BIN / 64;
Serial.println(x);// = 97
I need this to get the first digit of the binary code 1100001, so i thought just divide by 1000000, but thats not really working.
And one really confusing part for me is where i set x to Massage[0], BIN / 64 and i get a different result than in the line before, where i just printed it directly.
I hope someone can explain me why i get these results, or how i can get the first digit from a binary ASCII code like 1100001.
SurferTim:
If you mean the binary 1 in bit 6 of message[0], then this should do it.
char bitSix = message[0] >> 6;
That leaves only bit 6 in bitSix. It will be 1 if bit 6 was set and bit 7 is clear.
edit: That is if the first digit is the digit on the left. Or is it the binary 1 in bit 0? That is the first digit on the right.
i need the one on the left, but when i let print Massage[0] >> 6 it returns just a space, not a 1 or 0.
i just want to split Massage[0] wich is 97 and in binary 1100001
to another list, let it be BinMassage[] = {1,1,0,0,0,0,1}
and i thought i can just divide Massage[0], BIN with 1000000 to get the very left digit '1' from 1100001 and than put this in BinMassage[0], the next one i divide by 100000 and put it in BinMassage[1] and so on
DariusB:
I'm trying to send ASCII codes from one arduino to the other one via infra red. Therefor i need to convert the ASCII code to a binary code to send it the way i want to do it.
You want to send single bits as a value, starting from high-bit (bit-7) down to low-bit (bit-0)?
You can do it like that:
for (int i=7;i>=0;i--) Serial.print(bitRead(Massage[0],i));
Serial.print parameters like used in
Serial.println(Massage[0], BIN / 1000);
are nothing else than pure bullshit.
The second parameter of print can be HEX or BIN, but not "BIN / 1000".
Allowed second print parameters are BIN (for binary), OCT (for octal), DEC (for decimal) or HEX (for hexadecimal).
and i thought i can just divide Massage[0], BIN with 1000000 to get the very left digit '1' from 1100001 and than put this in BinMassage[0], the next one i divide by 100000 and put it in BinMassage[1] and so on
You don't know what a binary number is, do you ?
It would be a good idea to have 8 bits in your byte, rather than 7.
If you want to use your division scheme, you could write something like this
That should work, but would be rather inefficient.
i do know what binary number is and i know that "the next one i divide by 100000 and put it in BinMassage[1] and so on" was bullshit because it just wouldnt work, but i recognized that a bit too late^^.
now im doing it with bitRead, that works and is much better than my inefficient idea with dividing