Byte array to decimal

Hi,
I have a code where an array is printed. I want to create union of middle 4 bits and then convert to decimal.

void setup()
{
  // initialize both serial ports:
  Serial.begin(9600);         //monitor


}

void loop()
{
  byte message[] = {0xF2, 0xFE, 0x01, 0x02, 0x03, 0xD6, 0x65};

  for (int i = 0; i < sizeof(message); i++)
  {
    Serial.print(message[i], HEX);
    Serial.print(" ");
  }
  Serial.println(" ");
  delay(100);  //delay to allow host procesing

}

Output looks like:

F2 FE 1 2 3 D6 65  
F2 FE 1 2 3 D6 65  
F2 FE 1 2 3 D6 65  
F2 FE 1 2 3 D6 65  
F2 FE 1 2 3 D6 65  

...

What I want to get is 123D6 and then convert to decimal i.e. 74710.

Thanks in advance for your help.

Is it 0x123, or 0x010203?

0x123 but would love to learn how to deal with 0x010203 as well for a different scenario.

I want to add that the values in the byte array would change depending on the sensor output.

Are you absolutely sure of that format? Very odd to take the lower four bits of the first three bytes, then the full eight bits of the last byte. 0x123D6 send as four bytes would usually be 0x00 0x01 0x23 0xD6.

1 Like

middle 4 bits

Did you mean bytes?

What people are asking is "what do you really want to do?"

@david_2018 yes you are correct. I checked the checksum. it is 0x010203 actually.

@jremington yes middle 4 bytes - message is a byte array

To combine indices 2-5 and convert to decimal.

Why do want to convert to decimal?
The value is the same, whatever the base (radix)

Because correct sensor output would be 74710/100 (first post)

Have another guess at the correct answer.

I make 123D6 for you. You please try to make 74710 out of it.

byte message[] = {0xF2, 0xFE, 0x01, 0x02, 0x03, 0xD6, 0x65};

void setup() 
{
  Serial.begin(9600);
  unsigned long y = ((((((uint32_t)message[2]<<4)+ message[3])<<4)+ message[4])<<8) + message[5];
  Serial.println(y, HEX); //shows; 123D6
 }

void loop() 
{
  
}
1 Like

Oops

unsigned long y = ((((((uint32_t)message[2]<<4)+ message[3])<<4)+ message[4])<<8) + message[5];
(uint32_t)message[2]<<4)  //0000 0000 0000 0000 0000 0000 0001 0000
+ message[3]                                                                                     0000 0010
----------------------------------------------------------------------------------------------------------
                                    0000 0000 0000 0000 0000 0000 0001 0010 << 4
====>                         0000 0000 0000 0000 0000 0001 0010  0000
+message[4]                                                                 0000 0011
------------------------------------------------------------------------------------------------------------
                                 0000 0000 0000 0000 0000 0001 0010  0011 << 8
  ====>                    0000 0000 0000 0001 0010  0011  0000 0000
+message[5]                                                                 1101 0110
------------------------------------------------------------------------------------------------------------------
            0000 0000 0000 0001 0010  0011  1101 0110 = 00010203D6 //edit
1 Like

@GolamMostafa
Thank you! You solved my problem.
For decimal, I just need

Serial.println(y, DEC);

If the array is slightly different (sensor output would change but below is just for sake of an example), then is there a more general solution?

byte message[] = {0xF2, 0xFE, 0x02, 0xA2, 0xC2, 0xD6, 0x65};

No, for decimal, you just need

Serial.println(y);

Sure, aren't both same?

They are the same, one is just longer to type :wink: (hence the just need)

1 Like

Yeah right.
Could you help with my post #16?