Hex to long

Hi all

I have a hex char array that i need to convert to a (char) decimal number:

char *p="A020A6"; 
unsigned long val = strtoul(p,NULL,16);
Serial.println(val);    // print "10494118"

strtoul works ok if my hex is up to 4 bytes, but i need to convert a hex with 6 bytes:

hex char -> "A020A60C74C2" result -> "176062085231810"

Any ideas?

but i need to convert a hex with 6 bytes:

hex char -> "A020A60C74C2"

Your counter isn't working. "A020A60C74C2" is 12 characters.

My hex value is a 6 bytes variable, each byte in hex format:

byte mac[6];
sprintf(buf, "%02X%02X%02X%02X%02X%02X", mac[5], mac[4], mac[3],mac[2], mac[1],mac[0]);
Serial.println(buf);      // prints "A020A60C74C2"

Unsigned long is four bytes on Arduino.

My hex value is a 6 bytes variable, each byte in hex format

So, what possible value will there be in converting "A020A60C74C2" to a long or long long?

Getting the original 6 bytes back will not be easy. Converting two characters at a time to a byte, and storing the byte in an array, is trivial.

i need to get this: -> "A020A60C74C2" and convert to this -> "176062085231810"

No need to get the original 6 bytes back

i need to get this: -> "A020A60C74C2" and convert to this -> "176062085231810"

Why?

Take the first 4 hex digits, parse to unsigned long, then store in a long long, bitshift left by 32 bits. Then parse the last 8 hex digits to an unsigned long and add it to the long long of the first part.

Why do you think that 12 successive hex digits, derived from six independent bytes, has anything to do with the decimal string "176062085231810"?

PaulS: Why?

I have some code that retrieves IMEI from a GSM modem and sends it to a webserver. I need to do the same with a WIFI modem, sending MAC address. I cannot change the webserver code, so i need to translate a MAC char array into a IMEI char array.

i have this: -> "A020A60C74C2" (hex representation) and need to convert to this -> "176062085231810" (decimal representation)

jremington: Why do you think that 12 successive hex digits, derived from six independent bytes, has anything to do with the decimal string "176062085231810"?

https://www.binaryhexconverter.com/hex-to-decimal-converter

Put hex on the left, it gives that number on the right

Put hex on the left, it gives that number on the right

Well, whoopee!

I'll bet that if you rearrange the hex digits, you will get a different decimal string.

jremington: Well, whoopee!

I'll bet that if you rearrange the hex digits, you will get a different decimal string.

Didn't understand your point...

I just need a way to convert a hex number (6 bytes) to a integer representation (15 digits)

Is this acting as anything more than a way to get a unique identifier from a MAC address? (that's all you can use it for, I think). If the server is doing any sort of validation on the IMEI number, to ensure that it's a valid IMEI number, it would likely reject fake IMEI's generated this way.

I just need a way to convert a hex number (6 bytes) to a integer representation (15 digits)

You need to get off the "6 bytes" crap. You have a 12 character hex representation of a value that you want to to convert to a integral data type. Converting each character to a byte, in the range 0 to 15, and bit shifting, with the appropriate casts, storing the result in the proper type (NOT long; you must use long long), will get you the integral value you are looking for.

Didn't understand your point...

I just need a way to convert a hex number (6 bytes) to a integer representation (15 digits)

People have asked you to explain why you think you need to do that, but you haven't bothered to answer the question.

So, why do you think you need to do that?

DrAzzy: Is this acting as anything more than a way to get a unique identifier from a MAC address? (that's all you can use it for, I think). If the server is doing any sort of validation on the IMEI number, to ensure that it's a valid IMEI number, it would likely reject fake IMEI's generated this way.

Server validation is not a problem, because identifier are checked against a database. I just need to "transform" a MAC into a IMEI (15 digits) so the system can work with GSM modem or WIFI modem. And as i said, i cannot change the way the system transmits that identifier, it is expecting a 15 digit

jremington:
People have asked you to explain why you think you need to do that, but you haven’t bothered to answer the question.

So, why do you think you need to do that?

See my posts #9 and #16
I have a system that sends a 15 digit ID (taken from GSM modem IMEI) to a webserver.
I want the system can work with GSM or WIFI modem, so i have to send a 15 digit ID (taken from WIFI MAC address)

This compiles and shows one way to do it:

void setup() {
  Serial.begin(115200);
  unsigned long long value = convertHex("A020A60C74C2");
  
  // Note: .print() and .println() only go up to 'unsigned long' 
  // (9.5 decimal digits) so you can't display 'unsigned long long' 
  // directly.  You can get the bottom 9 digits with modulo and the 
  // upper bits with a divide.
  Serial.print((unsigned long) (value / 1000000000UL));
  // WARNING: Have to add code to display the leading zeroes!
  Serial.println((unsigned long) (value % 1000000000UL));
}
void loop() {}


unsigned long long convertHex(const char *p) {
  unsigned long long val = 0;
  while (*p) {
    val <<= 4;
    if (*p >= '0' && *p <= '9')
      val += *p - '0';
    else if (*p >= 'A' && *p <= 'F')
      val += (*p - 'A') + 10;
  }
  return val;
}

That worked, many thanks.

You missed p increment in your code, just before the while end Should be:

      val += (*p - 'A') + 10;
      p++;

Regards