Read mileage from byte array

Hey guys,

I'm trying to get my cars mileage out of the following byte array

80 0A BF 17 25 23 03 00 4C 72 CC D5

The mileage is in [4] [5] [6] in reversed order.
so it's 032325 = 205621 (km..)

I'm having trouble concatenating these three bytes into one string, or whatever other variable type would work.

I've tried this

                unsigned long mileageHEX1 = word(packet[6], packet[5]);
                debugSerial.println(mileageHEX1, HEX); // Works fine of course
                unsigned long mileageHEX = word(mileageHEX1, packet[4]);
                debugSerial.println(mileageHEX, HEX);

Problem is that [6] falls of as soon as I concat mileageHEX1 and packet[4]

Does anyone have an idea how I can solve this?
I guest concat/word doesn't work with more than 2 bytes?

All your help is greatly appreciated!

why dont you create a function which adds 'em up byte by byte......
also does strcat() function work....i guess not.....
since you want it in string,right?....convert em to string and then add 'em
so far i know, word is a data type....

Hi sabishaw, thanks for your reply

I need to store the mileage in EEPROM, so actually it would be easiest to have it as a long.

I already created a function that can do this:

long mileage;
// Mileage
void set_Mileage(long val) {
  mileage = val;
  EEPROM.put(10, mileage); //write unsigned long number u want
}
long get_Mileage() {
  return EEPROM.get(10, mileage);
}

So it would be best if I can have the 3 bytes as a long...

EEPROM.get and put always got on my nerves so havent used much....
why dont you use EEPROM.write() and write it byte by byte, that way you can also debug where it goes wrong by putting print and delay after saving each byte.....then use EEPROM.read to read again byte by byte

The EEPROM part works fine.
I need to display the mileage in decimal to the user, so only saving and reading it from eeprom won't help with that i suppose.

Thanks for the suggestion though!

i m sorry i didnt understand your question then.......what exactly you want???
converting hex to decimal or what????
and what about concating
also did strcat work?

LuckHermsen:

                unsigned long mileageHEX1 = word(packet[6], packet[5]);

debugSerial.println(mileageHEX1, HEX); // Works fine of course
               unsigned long mileageHEX = word(mileageHEX1, packet[4]);
               debugSerial.println(mileageHEX, HEX);




Problem is that [6] falls of as soon as I concat mileageHEX1 and packet[4]

of course it will! a WORD is 2 bytes, isn't it?

why not use the union function (use uint32_t and a 4-byte array) if you really want to put those bytes together?

ah that explains it yes.

sherzaad:
of course it will! a WORD is 2 bytes, isn't it?

why not use the union function (use uint32_t and a 4-byte array) if you really want to put those bytes together?

How could I best do that?

typedef struct 
{
  uint8_t as_byte[sizeof(uint32_t)];
  uint32_t as_value;
}
cracked_t;

...

  cracked_t cracked;

  cracked.as_byte[0] = packet[4];
  cracked.as_byte[1] = packet[5];
  cracked.as_byte[2] = packet[6];
  cracked.as_byte[3] = 0;

  Serial.println( cracked.as_value );

...

The byte order may be wrong.

here's a snippet

    union { 
        unsigned long val; 
        unsigned char bytes[sizeof(unsigned long)];
    } mileage;

    mileage.bytes[0] = packet[4]; //least significant byte whichever that is in your "packet"
    mileage.bytes[1] = packet[5];
    mileage.bytes[2] = packet[3];
    mileage.bytes[3] = 0; // not necessary but here for good measure;

    return mileage.val;

hope that helps

   mileage.bytes[3] = 0; // not necessary but here for good measure;

If mileage is local / automatic that assignment is necessary.

In case the code is ever ported to a 64 bit processor this would be a better choice...

    mileage.val = 0;
    mileage.bytes[0] = packet[4]; //least significant byte whichever that is in your "packet"
    mileage.bytes[1] = packet[5];
    mileage.bytes[2] = packet[3];

LuckHermsen:
Hey guys,

I'm trying to get my cars mileage out of the following byte array

80 0A BF 17 25 23 03 00 4C 72 CC D5

The mileage is in [4] [5] [6] in reversed order.
so it's 032335 = 205621 (km..)

I'm having trouble concatenating these three bytes into one string, or whatever other variable type would work.

I've tried this

                unsigned long mileageHEX1 = word(packet[6], packet[5]);

debugSerial.println(mileageHEX1, HEX); // Works fine of course
                unsigned long mileageHEX = word(mileageHEX1, packet[4]);
                debugSerial.println(mileageHEX, HEX);



Problem is that [6] falls of as soon as I concat mileageHEX1 and packet[4]

Does anyone have an idea how I can solve this?
I guest concat/word doesn't work with more than 2 bytes?

All your help is greatly appreciated!

If I was writing the program, I would:

result = [6] + 10*[5] + 100*[4];

Paul

 0  1  2  3  4  5  6  7  8  9  .  1
80 0A BF 17 25 23 03 00 4C 72 CC D5

So...

packet[4] = 0x25 = 37
packet[5] = 0x23 = 35
packet[6] = 0x03 = 3

result = packet[6] + 10packet[5] + 100packet[4];

3 + 1035 + 10037 = 4053

so it's 032335 = 205621 (km..)

Just a skosh off. (@LuckHermsen's value is also incorrect.)

I'm sorry guys but I'm afraid and don't really get it...

This is above my skill so I guess I'll need some extra explanation...

packet[4] = 0x25 = 37
packet[5] = 0x23 = 35
packet[6] = 0x03 = 3

result = packet[6] + 10packet[5] + 100packet[4];
3 + 1035 + 10037 = 4053

How does 4053 become 205621 (km..)?


@Coding Badly, yes I made a typo...
032335 should be 032325

Post #12 is a direct response to post #11. Ignore it. It has nothing helpful to you. (Neither does post #11.)

Focus your attention on the code snippets in posts #9 and #8.