Serial.print(Value,HEX);

HI,

I wanted to test the serial.print(value,HEX) function to make sure, that the output from the routine always are 2 chars long ( eg: 0x00 --> 00 and 0xFF --> FF).

so I wrote the following test code:

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

void loop()
{
int n;

for (int n = 0; n <= 16; n++)
{
Serial.print(n,HEX);
}
Serial.println();
}

But the result(s) were not very amusing:

n = 0 --> 0 (* 1 char ) , ... n = 9 --> 9 ( 1 char ),
n = 10 --> A (
1 char ) , n =15 --> F ( 1 char *)

the foreleading ZERO is missing.

n = 16 --> 10 ( 2 chars)

In a way the function is working fine but I want the output AS:

0 --> 00 , 1 --> 01, ... 9 --> 09 .... all with a foreleading Zero.

Is there an option in this ser.prnt(Value,HEX) command so I can force 2 digits output (eg a foreleading ZERO when value is below 0x10 ?

or is there another way to get a 2 (ASCII) chars output.

Kristian AKA snestrup2016

If you do Serial.print(n, DEC);, do you get a leading zero or zeroes?

So what's the problem?

(It's very simple to add as many leading zeroes as you desire, it just takes a tiny bit of effort)

Try snprintf with a 0x specifier, or Serial.print a 0 if the value is less than 16.

If you do not know snprintf, use Google, Bing, or equivalent.

vaj4088:
Try snprintf with a 0x specifier, or Serial.print a 0 if the value is less than 16.

If you do not know snprintf, use Google, Bing, or equivalent.

I tried Google() and Bing() and they didn't work.

1 Like

Perhaps this:

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

void loop() 
{
int n;

  for (int n = 0; n <= 16; n++)
  {
    Serial.print("0x");
    Serial.print(n < 16 ? "0" : "");
    Serial.print(n, HEX);
    Serial.print(" ");
  }
  Serial.println();
}
2 Likes

HI,

AWOL :

Sorry, my fault: I should have specified that the purpose of the test was to find IF I could use the ser.prnt(Value,hex) function in my project. My (very) old ISA-486 Compaq shortcircuited in the PSU and killed my EPROM-burner (Sunshine 4c). Now I have 4 * 2716 EPROM's I MUST read (and later copy that HEX-string to FLASH-RAM). SO I plan to build a new-EPROM-READER using an 2650 MEGA and was testing IF the ser.prnt(Value,hex) function was useful. (later putting the HEX-results into an Intel HEX string and later transfer that to a PC using puTTY or another FILE -supporting monitor. Sad to say - but using the ser.prnt(... DEC) option is not considered very useful, as it gives me more work on the PC-side.

HI,

econjack:

Thanks a lot. Your code did the job. As written in my answer (above) it is crucial that
the data are correct. With your code they are . ALL of them..

Many many thanks

(Pseudo-kode: BEGIN )

For (N = 0; n < No Ending; n++)
{
Serial.print("MANY MANY THANKS ");

}

(Pseudo-kode: Stop !)

Snestrup2016

aarg:
I tried Google() and Bing() and they didn't work.

You didn't implement them properly, then. POST YOUR CODE. 8)

Bing worked great for me, with 157,000 (!) hits for snprintf. The very first hit (http://www.cplusplus.com/reference/cstdio/snprintf/)
was quite helpful.

Of course, the solution by econjack works as well. In fact, it looks a lot like

Serial.print a 0 if the value is less than 16

Good Luck!