hello
i want to convert a hex number in string to a decimal number in string i have this function but i dont know why it is not working. could you please help me?
String senderNumber = "2b39313134363539383536353631";
String decimalString = hexToDecimal(senderNumber);
String hexToDecimal(String senderNumber) {
String decimalString;
for (size_t i = 0; i < senderNumber.length(); i += 2) {
String hexByte = senderNumber.substring(i, i + 2);
// Serial.println(hexByte);
char hexByteChars[3];
hexByte.toCharArray(hexByteChars, 3);
Serial.println(hexByte);
unsigned long decimalValue = strtoul(hexByteChars, nullptr, 16);
// Serial.println(decimalValue);
decimalString += String(decimalValue);
}
return decimalString;
}
What does the function do and what do you expect it to do?
i want to convert this string which is in hex
String senderNumber = "2b39313134363539383536353631"; // +9114659856561 in decimal
to a decimal string
And whλt does your function do?
edit: could it be that you want to convert a string of hex numbers to a string of chars?
i explain it in comments
for (size_t i = 0; i < senderNumber.length(); i += 2) {
String hexByte = senderNumber.substring(i, i + 2); // seperating each 2 character of String and put it to another string (hexByte)
char hexByteChars[3];
hexByte.toCharArray(hexByteChars, 3); // converting hexByte to chararray
unsigned long decimalValue = strtoul(hexByteChars, nullptr, 16); //converting HEX to Decimal
decimalString += String(decimalValue); // put the decimalValue to decimalString
}
return decimalString; // returning the decimalString
}
i want to convert String of hex numbers to String of decimal
it seems that this part of function doesnt work correctly
i found the solution now its working
the problem was the plus sign "+" at the first of decimal number
here is the final code :
String senderNumber = "2b39313134363539383536353631";
char charArray[senderNumber.length() / 2 + 1]; // +1 for null terminator
hexToCharArray(senderNumber, charArray);
Serial.print("Hexadecimal String: ");
Serial.println(senderNumber);
Serial.print("Character Array: ");
Serial.println(charArray);
String resultString(charArray);
Serial.print("Result String: ");
Serial.println(resultString);
void hexToCharArray(const String& senderNumber, char* charArray) {
for (size_t i = 0; i < senderNumber.length(); i += 2) {
String hexByte = senderNumber.substring(i, i + 2);
charArray[i / 2] = static_cast<char>(strtol(hexByte.c_str(), nullptr, 16));
}
charArray[senderNumber.length() / 2] = '\0'; // Null-terminate the character array
}
Just for the record: you do not convert a string with a hex number to dec (which would have 114 bits), you convert a string of 2-byte hex to a string of 1 byte chars 
yeah i will remember that. thanks @zwieblum