Hex formatting

when printing to serial port like below

byte x = 255
Serial.print(x, HEX);

outputs FF as expected

but if x is 0 to 15

the output is 0 to F is there as easy way to make the output 00 to 0F

thanks

if (x<16) serial.print('0')
serial.print(x, HEX)

Also note you misspelled serial. If you put the capital S the compiler will give you an error.

thanks i will try that as for the misspelling

a capital S works and a small gives me an error

error: 'serial' was not declared in this scope

zarobhr:
when printing to serial port like below

byte x = 255

Serial.print(x, HEX);



outputs FF as expected

but if x is 0 to 15

the output is 0 to F
is there as easy way to make the output 00 to 0F

thanks

How about doing it the “right” way?

void setup()
{
Serial.begin(9600);
runtest();
}

void loop()
{
}

void runtest(void)
{
char buffer[8];
const char *mask = “%02X\n”;
byte x;
for(x=0; x<16; x++) {
sprintf(buffer, mask, x);
Serial.print(buffer);
}
}

Oooops. You're right about the capital S. I've got my head in a different program this morning.

My bad.

krupski:

zarobhr:
when printing to serial port like below

byte x = 255

Serial.print(x, HEX);



outputs FF as expected

but if x is 0 to 15

the output is 0 to F
is there as easy way to make the output 00 to 0F

thanks

How about doing it the “right” way?

void setup()
{
Serial.begin(9600);
runtest();
}

void loop()
{
}

void runtest(void)
{
char buffer[8];
const char *mask = “%02X\n”;
byte x;
for(x=0; x<16; x++) {
sprintf(buffer, mask, x);
Serial.print(buffer);
}
}

i have tried both ways the orginal suggestion and the “Right Way”

the both work but the “Right Way” uses 1520 more bytes than the other way. so for now i will do the wrong way, since space will be of importance

i am still open to other suggestions as well

thanks

Imho the code of Delta-G is most pragmatic (short and clear).
You might consider replacing the value 16 with its hex equivalent, and wrapping it in a function so it becomes reusable

serialHEX(byte x)
{
  if (x < 0x10) serial.print('0');
  serial.print(x, HEX);
}

an alternative (for your collection :wink:

serialHEX(byte x)
{
  char h[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};  // might be declared global 
  serial.print(h[x>>4]);
  serial.print(h[x&0x0F];
}

robtillaart: serialHEX(byte x)   serial.print(x, HEX);

I thought we'd already got past the correct spelling of "Serial".

(that was left in as an exercise :)

sprintf(destination,"write anything here %02X", source);

will do the same e.g 0x0F

sprintf(destination,"write anything here %04X", source);

will print to 4 places e.g 0x000F

zarobhr:
the both work but the “Right Way” uses 1520 more bytes than the other way. so for now i will do the wrong way, since space will be of importance
i am still open to other suggestions as well
thanks

Well, if you need all the 32K and are up against the edge, then sure do whatever you need to in order to save space.

But, if your sketch only takes up 8K but you can shrink it to 6K, why bother?

I like using “sprintf” because you can do more than just simple conversion to hex.

For example, to print something like “The sensor is at address 0x00FE, it is sampling at 1000 samples per second and the average reading is 1023”

Compare THIS:

Serial.print("The sensor is at address 0x");
if (address < 0x1000) {
    Serial.print("0");
}
if (address < 0x100) {
    Serial.print("0");
}
if (address < 0x10) {
    Serial.print("0");
}
Serial.print(address, HEX);
Serial.print(", it is sampling at ");
Serial.print(samprate, DEC);
Serial.print(" samples per second and the average reading is ");
Serial.print(average, DEC);
Serial.println("");

…to THIS:

char buffer[128];
const char *mask = "The sensor is at address 0x%04X, it is sampling at %d samples per second and the average reading is %d\n";
sprintf (buffer, mask, address, samprate, average);
Serial.print(buffer);

Need I say more?

Need I say more?

Think you made your point clear and your arguments are definitely valid.

Besides memory, performance can also be an issue. Not tested but I expect sprintf() will execute a bit slower than the "the pragmatic" code if done in a tight loop. [yes I know when printing over serial communication takes most time]

@krupski Sorry, i didn't realise you had already mentioned Sprintf, but you right it is more versatile