convert long to HEX-String

Hi there,

i'm trying to convert a long to a HEX-String, but i can't get the right result.
Maybe someone can help me find my mistake.
Here's the code:

String str;
int test = 1;
char out[20];
long strlint;

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

void loop() {
  if(test == 1){
  		str="0004572516";
    	Serial.print("str: ");
    	Serial.println(str);
    	strlint = atol(str.c_str());
    	Serial.print("strlint: ");
    	Serial.println(strlint);
    	Serial.print("String(strlint, HEX): ");
    	Serial.println(String(strlint, HEX));
    	Serial.print("sprintf(out,\"%08X\",strlint): ");
		sprintf(out,"%08X",strlint);
    	Serial.println(out);
  		test = 0;
  }  
}

and here's the output:

str: 0004572516 
strlint: 4572516 
String(strlint, HEX): 455555 
sprintf(out,"%08X",strlint): 0000C564

Actually i'm expecting 0045C564 as output... or at least: 45C564

Can anyone help me please?

Thanks in advance!

BirdyB:
Can anyone help me please?

When things like this happen, it is because you don't understand the functions you are using.
Read the documentation.

String(strlint, HEX) ? ? ?

First, the format string in your sprintf() needs the long specifier. In the code below, the sprintf() function compiles to 3576 bytes. The ltoa() version compiles to 2254 bytes:

void setup() {
  // put your setup code here, to run once:
  char buffer[15];
  long val = 4572516;

  Serial.begin(9600);

 // sprintf(buffer, "%09lX", (long)val);      // Use the long specificer before the 'X'
  ltoa(val, buffer, 16);
  Serial.println(buffer);
}

void loop() {
  // put your main code here, to run repeatedly:

}

sprintf() is a very powerful function, but rarely does one program use all of that power. You're usually better off using one of the *toa() functions. See:

http://www.jedsoft.org/slang/doc/html/slangfun-13.html

 // sprintf(buffer, "%09lX", (long)val);      // Use the long specificer before the 'X'

But why is it necessary to cast a long to a long?

PaulS:
But why is it necessary to cast a long to a long?

It isn't. I was experimenting to see if sprintf() was smart enough to convert from int to long if the variable was cast to a long. It isn't. I forgot to remove it.

If all he wants to do is print out the long as a string

long i = 4572516;

.
.
.
Serial.println(i, HEX);

ieee488:
String(strlint, HEX) ? ? ?

The reference page for the String constructor, here - https://www.arduino.cc/en/Reference/StringConstructor says this:

Syntax
...
String(val, base)

Parameters
val: a variable to format as a String - string, char, byte, int, long, unsigned int, unsigned long, float, double [emphasis added]
base (optional) - the base in which to format an integral value

Examples
All of the following are valid declarations for Strings.
...
String stringOne = String(45, HEX);

So, it looks like that construction is valid for an integer, and it looks like the constructor can manage long's and unsigned long's. I can't find anything wrong with it. Can you elaborate?

ieee488:
If all he wants to do is print out the long as a string

I thought that the fact he said "convert" rather than "display" meant he wanted to use the string for something later in the code.

econjack:
First, the format string in your sprintf() needs the long specifier. In the code below, the sprintf() function compiles to 3576 bytes. The ltoa() version compiles to 2254 bytes:

What size does PString compile to?

I don't like Strings, but conversions to and from strings in hexadecimal are quite easy.

unsigned long inValue = 0xDEADBEEF;
char inValChars[9];

unsigned long outValue;
char outValChars[9];

void oneNibble(char*& store, byte val) {
  val &= 0xF;
  *store++ = (val < 10 ? '0' : 'A' - 10) + val;
}
void oneByte(char*& store, byte val) {
  oneNibble(store, val >> 4);
  oneNibble(store, val);
}
void oneULong(char* store, unsigned long val) {
  oneByte(store, val >> 24);
  oneByte(store, val >> 16);
  oneByte(store, val >> 8);
  oneByte(store, val);
  *store = 0;
}
unsigned long readULong(char* from) {
  unsigned long result = 0;
  while (isxdigit(*from)) {
    byte xdigit = toupper(*from++);
    result <<= 4;
    result += xdigit - (xdigit < 'A' ? '0' : 'A' - 10);
  }
  return result;
}
void setup() {
  Serial.begin(115200);
  Serial.print(F("ulong "));
  Serial.print(inValue, HEX);
  Serial.print(F(" -> '"));
  oneULong(inValChars, inValue);
  Serial.print(inValChars);
  Serial.println(F("'"));
  Serial.print(F("'"));
  Serial.print(inValChars);
  Serial.print(F("' -> ulong "));
  outValue = readULong(inValChars);
  Serial.print(outValue, HEX);
  Serial.println();
}
void loop() {}
ulong DEADBEEF -> 'DEADBEEF'
'DEADBEEF' -> ulong DEADBEEF