Converting unsigned long long to char array and back

Hi all,

I’m trying to convert an unsigned long long integer to a char array and back.
Up to specific number of digits in the integer the conversion is done properly, but when the digits are more than that the back conversion from char array to unsigned long long is not correct (although the conversion from integer to char array is correct).
I was thinking I might exceed the max limit of unsigned int, but seems not.
Below is the code. Can anyone help me to figure out?

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() 
{

  unsigned long long int iData=12345612345612; // when the value is 1234561234561 the conversion is correct
  unsigned char cDataBuffer[8];
  for (int i = 0; i < 8; i++) //convert to char array
  {
    cDataBuffer[i] = ((iData >> (i * 8)) & 0XFF);
    Serial.print(cDataBuffer[i],HEX);
  }
  unsigned long long ab = *((unsigned long long*)cDataBuffer); // convert to long long
  
  
  Serial.println("long=");
  Serial.print(long(ab/1000));
  Serial.println(long(ab%1000));
  delay(1000);
}
iData=12345612345612

This is fourteen decimal digits. Even after dividing by 1000, that exceeds the ten digits unsigned long can hold.

You might be better off using the BigNumber library. https://www.gammon.com.au/forum/?id=11519

void setup() {
  Serial.begin(250000);
  unsigned long long iData = 12345612345612; // when the value is 1234561234561 the conversion is correct
  unsigned char cDataBuffer[8];
  Serial.print("hex  = ");
  for (int i = 0; i < 8; i++) //convert to char array
  {
    cDataBuffer[i] = ((iData >> (i * 8)) & 0XFF);
    if (cDataBuffer[i] < 16) {
      Serial.write('0');
    }
    Serial.print(cDataBuffer[i], HEX);
  }
  unsigned long long ab = *((unsigned long long*)cDataBuffer); // convert to long long

  Serial.println();
  Serial.print("long = ");
  Serial.print(long(ab / 1000000));
  Serial.println(long(ab % 1000000));
}

void loop(){
}
hex  = 0CA1D66F3A0B0000
long = 12345612345612

It is not neccessary to copy the long long to access it as bytes

void setup() {
  Serial.begin(250000);
  unsigned long long iData = 12345612345612; // when the value is 1234561234561 the conversion is correct
  unsigned char* cData = (unsigned char*) &iData;
  Serial.print("hex  = ");
  for (int i = 0; i < 8; i++) {
    if (cData[i] < 16) {
      Serial.write('0');
    }
    Serial.print(cData[i], HEX);
  }
  unsigned long long ab = *((unsigned long long*)cData);
  Serial.println();
  Serial.print("long = ");
  Serial.print(long(ab / 1000000000UL));
  Serial.println(long(ab % 1000000000UL));
}
void loop() {}
hex  = 0CA1D66F3A0B0000
long = 12345612345612

if (cData < 16) {

  • Serial.write(‘0’);[/quote]*
    Thanks Whandall for fast reply. Your code works just fine. But I couldn’t understand the quoted part of code. Can you please explain?

If you print values (even in HEX), leading zeros are suppressed.

So 0x01020304 would print as 1234 (if printed byte by byte).

If the value is below 16 (which means only one digit) the leading zero is inserted by that code.

Got it. Thanks