Go Down

Topic: Adding a leading 0 to incoming HEX data for conversion to binary (Read 3193 times) previous topic - next topic

Sme1986

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

MarkT

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.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Sme1986

Hi Mark!
Thanks for your reply!
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[0] to rxBuf[8].

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.


aarg

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?
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

Sme1986

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.

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. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Sme1986

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.

PaulS

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

Sme1986

Hi Paul!
Ok, then - how do I get the whole CAN Message converted to Binary with the leading zeros?
So:
Code: [Select]
A7 03 B4 9D 00 BB 07 7D

Should be

Code: [Select]
10100111000000111011010010011101000000001011101100000111011111101


If I tried by just saying:

Code: [Select]
   

 for(byte i = 0; i<len; i++)
{
    
    Serial.print(rxBuf[I],BIN);
    
}



But - the leading zeros are not there :(

evanmars

How are you 'displaying in binary'?
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.  

jremington

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

Delta_G

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.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

sherzaad

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

aarg

Hi Paul!
Ok, then - how do I get the whole CAN Message converted to Binary with the leading zeros?
So:
Code: [Select]
A7 03 B4 9D 00 BB 07 7D

Should be

Code: [Select]
10100111000000111011010010011101000000001011101100000111011111101
But my question is, what are you doing with the string 10100111000000111011010010011101000000001011101100000111011111101?
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

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.

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 :

Code: [Select]
HEX: A7 03 B4 9D 00 BB 07 7D

This would be in BIN

Code: [Select]
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!

Go Up