# Adding a leading 0 to incoming HEX data for conversion to binary

Hello again!
I am still working on my CAN-Bus project (btw I will of course post the finished code if anyone is interested) from this Link here.

As maybe the whole problem is just too much to ask for here is a little sub-question:

The CAN-Message I receive is in Hex. As long there is no leading “0” in the byte everything is fine.
For instance if I get a “AF” I can convert this to Binary (which would be “1010 1111”).
My Problem is, if I get a single Digit HEX Value (for instance “03” wheres the 0 is not transmitted and I only get a 3).
So a 03 would be “0000 0011” – that’s what I need. What I get is “0011”, as the leading 0 is not in my Buffer.
Any suggestion on how to get this done?

Thanks for any input What do you actually means by "is in hex"? An ASCII string "AF", ie the two bytes 0b01000001, 0b01000110 ? Or
is this a binary value already, in which case the description isn't meaningful, internally values are always
binary.

Hi Mark!
So basically what I get from the mcp_can.h library is a char array of 8 Bytes, representing the CAN-Message I get from the Car.

For instance this could be

"A7 03 B4 9D 00 BB 07 7D"

The message is saved in the rxBuf to rxBuf.

I need to convert this message to binary to then again assemble the bits I need for a specific information (example: Startbit 11, Length 9).

So, for the first 2 bytes the binary should look like this:

"0B1010011100000011"

The Problem is, that as soon as there is a leading 0 in my Hex message (the second byte for example) it gets left out.

So my 0x03 converts to 0B0011 instead of 0B00000011

The zeros ahead of the 0B0011 include important information for me (example: Is the blower on or off).
Is there any way to (memory efficiently) "copy" the message into the binary format with access to each separate bit?

Thanks for your help, appreciate it.

Sme1986:
So my 0x03 converts to 0B0011 instead of 0B00000011

0B0011 and 0B00000011 are exactly the same number. Why do you need an ASCII representation of a binary number anyway?

Hi aarg,
yes, if you see it in total they are the same number.
Let me try to explain:

0x03 should be 0B00000011
In this message the Bit 1 (so a "0") could represent the status of the blower (on->1 off->0) of the ABS, or so on...
So you see, even though there are only "0000" this could represent the Boolean information on 4 actuators.

Hope I could explain this understandably.

The fact that the print method doesn’t print the leading zeros doesn’t mean they’re not there. Everything in an Arduino is 8bits wide.

Hi Delta_G,

well, I thought so, too! But just plainly printing out the received HEX values I get:

A7
3
B4
9D
0
BB
7
7D

--> If I display this in binary I am missing exactly those leading zeros.

If I display this in binary I am missing exactly those leading zeros.

The key word in that phrase is DISPLAY. The 0s are NOT missing. They are just not necessary to convey the required information. 0x3 is the EXACT same value as if you typed 0x00000003.

Hi Paul!
Ok, then - how do I get the whole CAN Message converted to Binary with the leading zeros?
So:

``````A7 03 B4 9D 00 BB 07 7D
``````

Should be

``````10100111000000111011010010011101000000001011101100000111011111101
``````

If I tried by just saying:

`````` for(byte i = 0; i<len; i++)
{

Serial.print(rxBuf[I],BIN);

}
``````

But - the leading zeros are not there How are you 'displaying in binary'?

Delta_G:
The fact that the print method doesn't print the leading zeros doesn't mean they're not there. Everything in an Arduino is 8bits wide.

``````for(byte i = 0; i<len; i++)
{
char buf;
sprintf(buf,"%02x ",rxBuf[i]);
Serial.print(buf);
}
``````

What's more, if you have an int with the vale 0x3 and you want it to have binary 00000011 then good news. You don't have to do ANYTHING. Everything is stored in binary. You can print it or write it as hex for human consumption, but inside the chip it is ALL binary.

check out this post on how to add zero padding for binary string display

Sme1986:
Hi Paul!
Ok, then - how do I get the whole CAN Message converted to Binary with the leading zeros?
So:

``````A7 03 B4 9D 00 BB 07 7D
``````

Should be

``````10100111000000111011010010011101000000001011101100000111011111101
``````

But my question is, what are you doing with the string 10100111000000111011010010011101000000001011101100000111011111101?

Hi all!
Thanks a lot for your help.

@aarg: What I doing it reading CAN-Bus Data from a Car. I get 8 * 8Bytes of Data, saved in an Array "rx_frame.dada.u8"

So in total, I receive 64 Bits of information.

These Bits could represent the state of 64 actuators in terms of "on/off --> 0/1.
Usually though the 64Bits represent measurement Values of some sort. So, for instance we could receive a message :

``````HEX: A7 03 B4 9D 00 BB 07 7D
``````

This would be in BIN

``````10100111 00000011 10110100 10011101 00000000 10111011 00000111 01111101
``````

Now lets suspect there are 2 known signals in this message.

• 1 is an On/Off state
• 1 represents the speed of the car

The CAN Database could say:

Signal 1: Startbit 0, Length 1
Signal 2: Startbit 11, Length 9

For Signal1 I would need to take the first Digit of the Binary - pretty simple.
For Signal2 I would need to take "000011 10110", and convert this to a Decimal Number, multiply with a Faktor and Offset it. If there was no Faktor or Offset the correlating DEC would be 118.

So, for what I see I have to accomplish the following tasks:

1. Take all the Data of the Array "rx_frame.dada.u8" and put it together in one variable.
2. I need to somehow sort out the Bits I need (--> Bitshifting?) into a separate variable
3. Convert the picked out bits to a decimal value.
4. Faktor and Offset the value.

3 and 4 is clear - 1/2 not yet.

Hope I could explain what I am trying to do...

Very thankful for any input!

You're still talking about converting a binary number to decimal to do some math. That's where you misunderstand. A computer CANNOT do math in decimal. They only understand binary. You can display a number however you want but the numbers themselves are all represented in binary for the processor. You don't need to convert anything to do math with it. Math is the same in any base anyway. The only time comverting bases comes into play is when you want to display a number for a human.

You have 64 bits of data. Use a little math, to determine which byte the data is in, and bitRead() to read the bit from the byte. Do that as many times as needed to get all the bits that make up a value. Then, just use the value. Do not concern yourself with whether the bits represent a binary value, a decimal value, or a hexadecimal value.

Sme1986:
Hi all!
Thanks a lot for your help.

@aarg: What I doing it reading CAN-Bus Data from a Car. I get 8 * 8Bytes of Data, saved in an Array "rx_frame.dada.u8"

So in total, I receive 64 Bits of information.

But as others have pointed out, you are not receiving CAN data as a string of binary characters. You are receiving it as a series of bytes, which have all 8 bits, not just the bits that happen to get printed by the Arduino binary print method.

The real answer to my question, which is what are you doing with the binary string, is "nothing except looking at it and being confused". Because, the binary string is just one of many ways of printing it.

You may find this function useful: BitRead