How can I force a print format?

I have sketched a quick program to output 128 HEX integers "0-7E" . The problem is that I would like to have 00,01,02,.....0F etc. for the integers below 10. Here is what I am getting:

0'1'2'3'4'5'6'7'8'9'A'B'C'D'E'F'10'11'12'13'14'15'16'17'18'19'1A'1B'1C'1D'1E'1F'20'21'22'23'24'25'26'27'28'29'2A'2B'2C'2D'2E'2F'30'31'32'33'34'35'36'37'38'39'3A'3B'3C'3D'3E'3F'40'41'42'43'44'45'46'47'48'49'4A'4B'4C'4D'4E'4F'50'51'52'53'54'55'56'57'58'59'5A'5B'5C'5D'5E'5F'60'61'62'63'64'65'66'67'68'69'6A'6B'6C'6D'6E'6F'70'71'72'73'74'75'76'77'78'79'7A'7B'7C'7D'7E'

Here is the code:

/*
  Serial Call and Response in ASCII
 L
 */

int talk = 0;    // output int
int inByte = 0;  // just a place to store input for later

void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
  

}

void loop()
{
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
  while (talk < 127){  
    Serial.print(talk, HEX);  
    Serial.print("'");
talk = talk + 1;    
  }

}
}

Thanks for any help'
Mark

How about:

while (talk < 127){  
    if (talk < 16) Serial.print("0");
    Serial.print(talk, HEX);  
    Serial.print("'");
talk = talk + 1;    
  }

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons

Thank you for the bullet fast response! I put the code in and it is exactly what I wanted!

One of these days I will realize that not everything has to be difficult. :blush:

Thanks,
Mark

sprintf() might do that if you specify leading zeros.

The previous response is, however, way more efficient. :slight_smile:

bubulindo:
sprintf() might do that if you specify leading zeros.

The previous response is, however, way more efficient. :slight_smile:

I would check out sprintf since if I end up trying to do 3-digit hex numbers, I'll be making minimal changes from 2-digits if I used sprintf :slight_smile:

Thanks for the idea of sprintf(). I will test that out too. I think I have seen that used in a sketch posted on the forum.

Thanks,
Mark

on printf it would look like:

printf("%02X \n", 10);

I saw this somewhere a few days ago. I'm not sure if it was here or on a general programming forum. Give it a go.

bubulindo:
on printf it would look like:

printf("%02X \n", 10);

I saw this somewhere a few days ago. I'm not sure if it was here or on a general programming forum. Give it a go.

printf in regular computers outputs to default output device, the screen. But since arduino has no default output device, it should do nothing even if it exists. You should use sprintf, which outputs to string, thus the s-printf. Just like scanf would scan from default input, the keyboard, on a regular computer, and doesn't do anything on an arduino. But if you want to recognize numbers, say from serial input, you can do sscanf, the string scanf to pick numbers from a sentence.

printf() can be set to work (to certain extent) - Arduino Playground - Printf -