 # division with binary codes

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, BIN = 1100001 and Massage, BIN / 10 = a (the parts after the ‘=’ are just representing what was printed)

``````    Serial.println(Massage);//                        =97(ASCII code von 'a')
Serial.println(Massage, BIN);//                   = 1100001
Serial.println(Massage, BIN / 1);//               = 1100001
Serial.println(Massage, BIN / 10);//              = a
Serial.println(Massage, BIN / 100);//             = a
Serial.println(Massage, BIN / 1000);//            = a
Serial.println(Massage, BIN /  1000000);//        = a
Serial.println(Massage, BIN / B1000000);//        = a
Serial.println(Massage, BIN / 64);//(64=0b1000000)= a
x =            Massage, 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, 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.

What do you mean by “the first number”? You mean the value in bit 6?

sorry, i mean the first digit of 1100001

First decimal, hex, or binary digit?

If you mean the binary 1 in bit 6 of message, then this should do it.

`````` char bitSix = message >> 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.

Ascii codes are binary codes.

Everything in the computer are binary codes.

Therefor i need to convert the ASCII code to a binary code

Why?

Serial.println(Massage); This sends 97 to the other computer, what's wrong with that?

In the other computer you will read 97 which is "a", you can do what ever you want with it there.

Keep in mind:
Serial.println(Massage, BIN);

Sends 1 1 0 0 0 0 1 as 7 digits.

LarryD: Keep in mind: Serial.println(Massage, BIN);

Sends 1 1 0 0 0 0 1 as 7 digits.

Actually, it sends those digits as ascii. So lots of 48 and 49.

OP, numbers is numbers is numbers and they're all binary to computers. There's no reason to convert anything.

SurferTim: If you mean the binary 1 in bit 6 of message, then this should do it.

`````` char bitSix = message >> 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 >> 6 it returns just a space, not a 1 or 0. i just want to split Massage 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, BIN with 1000000 to get the very left digit '1' from 1100001 and than put this in BinMassage, the next one i divide by 100000 and put it in BinMassage 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,i));
``````

Serial.print parameters like used in

``````Serial.println(Massage, 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).

well thank you, i didnt know about stuff like bitRead, ill try it with this

i just want to split Massage wich is 97 and in binary 1100001
to another list, let it be BinMassage = {1,1,0,0,0,0,1}

I don’t know why you want to do it but try this

``````byte Massage = {97};
byte binMassage;

void setup()
{
Serial.begin(115200);
Serial.println(Massage);
Serial.println(Massage, BIN);

for (int bit = 7; bit >= 0; bit--)
{
}

for (int bit = 7; bit >= 0; bit--)
{
Serial.print(binMassage[bit]);
}
}

void loop()
{
}
``````

and i thought i can just divide Massage, BIN with 1000000 to get the very left digit ‘1’ from 1100001 and than put this in BinMassage, the next one i divide by 100000 and put it in BinMassage 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

``````unsigned char Massage = 97 ;
unsigned char BinMassage ;

unsigned char divisor=128 ;

for ( int i=0 ; i<8 ; i++ )
{
if ( Massage/divisor == 1 )
{
BinMassage[i] = 1 ;
Massage -= divisor ;
}
else
{
BinMassage[i] = 0 ;
}
divisor /= 2 ;
}
``````

That should work, but would be rather inefficient.

michinyon:
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

``````unsigned char Massage = 97 ;
``````

unsigned char BinMassage ;

unsigned char divisor=128 ;

for ( int i=0 ; i<8 ; i++ )
{
if ( Massage/divisor == 1 )
{
BinMassage[i] = 1 ;
Massage -= divisor ;
}
else
{
BinMassage[i] = 0 ;
}
divisor /= 2 ;
}

``````

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 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