[SOLVED] Long to Hex in array

Hi guys,

i try to convert from Long to Hex to send by Serial but in Byte :

long temLong = 34999 ;  //the float temperature after convert

//Debug : convert long to Hex

String temStringHex = String (temLong, HEX);
    
Serial.print(temStringHex);// Display : 88b7

String is good but that's i would have is to send the result of convert long to Hex like this:

byte temHex[] {0xb7,0x88};

 Serial.write(temHex, sizeof(temHex));

How could i do ? Thanks

long hexValue = 0xdeadbeef;
Serial.write ((byte*)&hexValue, sizeof(hexValue));

Thanks for reply, but what the step before how did you find the result "0xdeadbeef" :

how do i do to convert 34999 to place it on this format 0x88b7 ?

is it :

long temHex = (temLong, HEX);

 Serial.write ((byte*)&temHex, sizeof(temHex));

i'm right ?

is it :

If you want temHex to equal 16, that is the way to do it.

i'm right ?

I have no idea now what it is you want to do.
Are you saying you want 34999 printed as four bytes worth (eight characters) of ASCII hex, or you just want to send four binary values over serial?

PaulS:
If you want temHex to equal 16, that is the way to do it.

unfortunately i want : long 34999 => Hex 0x88b7 (32 bits)

How to send the convert from long to Hex (in bytes) like this ?

byte temHex[] {0xb7,0x88};

 Serial.write(temHex, sizeof(temHex));

unfortunately i want : long 34999 => Hex 0x88b7

You need to no nothing to make that happen. Hex, decimal, octal, or binary are ways of viewing the value. The Arduino only knows one way to store the data - binary.

Why are you using a signed long to store the value? An unsigned int would make more sense, unless the values will exceed 65535, in which case sending two bytes won't be useful.

  unsigned int val = 34999; 
  Serial.write(highByte(val));
  Serial.write(lowByte(val));

TolpuddleSartre:
just want to send four binary values over serial?

Yes, i got a long 34999. That i want is to send the result of convert 34999 to Hex (88b7)like this

byte temHex[] {0xb7,0x88};

 Serial.write(temHex, sizeof(temHex));

PaulS:
You need to no nothing to make that happen. Hex, decimal, octal, or binary are ways of viewing the value. The Arduino only knows one way to store the data - binary.

Why are you using a signed long to store the value? An unsigned int would make more sense, unless the values will exceed 65535, in which case sending two bytes won't be useful.

  unsigned int val = 34999; 

Serial.write(highByte(val));
  Serial.write(lowByte(val));

I send temperature via protocole on 2 bytes . I found this way :

Ex : 12.45°c => x100 = 1245 => 1245-32767 = 34012 => 0x84DC

So the goal is to transmit on serial (in Byte) {0xDC,0x84}

2 bytes

That would be the size of a signed or unsigned int, not a long. Quit confusing the issue by using longs.

  float temp = 12.45;
  int val = temp * 100 - 32767; // 0x84DC 
  Serial.write(lowByte(val)); // Send 0xDC
  Serial.write(highByte(val)); // Send 0x84

Forgot the code tags...

PaulS:
Quit confusing the issue by using longs.

That's sound good !!!

    float temp = -12.45;
    unsigned int val = temp * 100 + 32767; // (i've made mistake it's +32767) 0x7b22
    String temStringHex = String (val, HEX);
    Serial.print(temStringHex); Serial.print(' ');      // 7b22
    
    Serial.write(lowByte(val)); // Send 0x22
    Serial.write(highByte(val)); // Send 0x7b

Very good !! Thanks a lot !