chipKIT® Development Platform

Inspired by Arduino™

Sprintf hexadecimal character representation - Leading Zero

Created Fri, 06 Jan 2012 19:06:26 +0000 by krosscbd


krosscbd

Fri, 06 Jan 2012 19:06:26 +0000

Greetings all,

I'm working with a ChipKit Max32 board, and I'm encountering an issue wherein if I use the sprintf function to write hexadecimal characters to a buffer it adds an additional zero to the beginning of each hexadecimal character and doesn't seem to follow the correct sytax of the sprintf function.

An additional user has experienced this same issue and posted about it on the Arduino forums as well:

[url]http://arduino.cc/forum/index.php?topic=69708.0[/url]

The objective is to write out a string on the serial port with a timestamp, CAN PGN, and 8 data bytes all in hexadecimal format. In the code example below, bdkbuff is a character array, pgn is an integer, and each of the message->data elements are of type byte.

Using either %X, %x, %2X, or %02X inside of the sprintf function yields similar results:

0028269 0FFFF 0FF 0FF 0FC 09 0FF 0FF 0FF 0FF

All of the Hexadecimal characters seem to be correct, however any two digit hex character has a leading zero in the string.

Ideally, I'm looking to print the following out to the serial port:

0028269 FFFF FF FF FC 09 FF FF FF FF

I don't seem to have an issue running this exact same code with a standard Arduino - it seems to be somehow specific to the Chipkit MPIDE.

Just curious if anyone has seen this issue before or could perhaps shed some light on what might cause this?

I absolutely love the ChipKIT 32 platform and it has worked flawlessly for CAN/USB and other applications I've done. It's a great board!

Thanks for any insight anyone might be able to provide.

-Brian

char bdkbuff[100];
int pgn;
sprintf(bdkbuff,"%07d %05X %02X %02X %02X %02X %02X %02X %02X %02X",millis(),pgn,message->data[0],message->data[1],message->data[2],message->data[3],message->data[4],message->data[5],message->data[6],message->data[7]);
Serial.println(bdkbuff);