How to use Serial.print returning full HEX value?

Hi guys - I've been looking through documentation, and the forums, and Google, and I can't seem to find out how to do what I want to do. So, I'm asking for help :-)

I'm working with Serial.print, and I am not getting the desired output that I want. Regardless of what data type I pass to Serial.print, I want to receive a full word value back. Basically, I with what I'm doing, I always want the Arduino to return all four characters of the value, even if their values are zeros.

For example, I have an unsigned int, and I want to return it as a full word length hex value - Is this possible with the Arduino regardless of what the value is? If the value for my unsigned int is "10", then I want the Arduino to print "000A". Currently, if I Serial.print(10, HEX); the Arduino just returns "A" as the value.

Is there a way for me to return the data I want to return?

I'm probably answering my own question, but I really don't want to have to go through and bit shift these values... I bet that's what I'm going to have to do...

You might want to give printf() or sprintf() a try.

printf() allows you to format a string according to values and format specifiers. An example is as follows: ``` printf( "%4X", 10 );  // outputs '000A' ``` Where %4X is the format specifier (a 4-character long hexadecimal value) and 10 is the value that you want to use. Note that if you want it to be part of a larger string, you can do so like this: ``` printf( "My hex value is %4X.", 10 );  // outputs 'My hex value is 000A.' ``` [edit]Just reading through some other threads, printf() will not work for you, as printf() is engineered to print to stdout (which does not exist for the arduino). Instead, you'll need to use sprintf(), which performs the string formatting and spits it out as a return value. eg: ``` mystring = sprintf( "Hex %4X wowie", 10 ); Serial.print( mystring ); ``` Or something similar. I'm not well versed in Arduino, but theoretically that should get you close to what you need.[/edit]

Vector: Thanks for the reply - You totally got me on the right track!

For anyone else that runs into this, here’s what I ended up doing:

#include <stdio.h>

unsigned int testVal = 0;

void setup(){
 Serial.begin(9600);
 Serial.println("Start!");
}

void loop() {
  if (testVal <= 255) {
    char charVal[4];
    sprintf(charVal, "%04X", testVal);
    Serial.println(charVal);
    testVal++;
  }
}

This simply counts from 0 through 255, and outputs the values as a full word in Hexadecimal.

Skizzo, One suggestion, with %04X you should change charVal[4] to at least charVal[5] since sprintf will add a trailing NUL character on its "output" of four hex characters. While it doesn't appear to be bothering your program now, if some other data is allocated in memory after charVal it could be corrupted by the implicit NUL char you're not accounting for. Often this will lead to bugs that are difficult to find -- especially for the beginner. Trust me on this! :-)

-Rusty-

Rusty - Thanks for the clarification and suggestion.

I knew about that trailing null at one point in time, but completely forgot about it. Oops ::)