Problem - Hex array is printing decimal equivalents in serial monitor

Please help... in the code below I'm trying to serial print :
02 63 6e 00

However, I'm getting:
2
99
110
0

It's printing the decimal equivalents. How can I get the desired output and keep it on the same line?
Thx in advance for you help.

unsigned char Foo[4] = {0x02,0x63,0x6e,0x00};

void setup(){

Serial.begin(19200);

delay(3000);

int i;
for (i = 0; i < 4; i = i + 1) {
Serial.println(Foo*);*

  • }*
    }
    void loop(){
    {

Why are you trying to print it 4 times ?

I am wondering if your problem is because you declared it as "unsigned". A text array is normally just plain char[]

I think you want this:

for ( int i=0 ; i<3 ; i++ ) 
{
      Serial.print(  foo[i], HEX ) ;
      Serial.print(" ");
}
Serial.println();

Numbers are just numbers, and there's no way for the Arduino to know that you used a hex value for initialization instead of a decimal, octal, or binary value. So you have to explicitly tell print() how you want it printed, as michinyon says...

Thanks michinyon and westfw for your help. The output is now hexadecimal. I'm getting:
2 63 6E 0

However, I also need the leading zero for the 2 and 0 ; like so:
02 63 6E 00

I can't figure out why it's being drop. How can I remedy this. Again, thanks in advance for your support. This is by far the best forum I've ever experienced. Here's my updated code:

char foo[4] = {0x02,0x63,0x6e,0x00};

void setup(){

Serial.begin(19200);

delay(3000);

for ( int i=0 ; i<4 ; i++ )
{
Serial.print( foo*, HEX ) ;*

  • Serial.print(" ");*
    }
    Serial.println();
    }
    void loop(){
    }
if (foo [i] < 16)  {
  Serial.print ("0");
}

Please use code tags when posting code.

Thanks AWOL, where in the sketch should I insert the code?

Guess.

AWOL, I'm a beginner with the C language and require quite a bite of patience from you guys (Lol :)). I've tried many combinations, but not getting the desired output. Here's the best combination so far, but it's dropping the 63 and 6E. I'm getting

02 00

Here's my code:

char foo[5] = {0x02,0x63,0x6e,0x00, 0x00};

void setup(){

  Serial.begin(19200);   
  delay(3000);
    
    for ( int i=0 ; i<4 ; i++ ) {
    if (foo [i] < 16) {
      Serial.print ("0");
      Serial.print(foo[i], HEX ) ;
      Serial.print(" ");   
  }
}  
      Serial.println();
}

void loop(){
}

The code I wrote fits as written into the code you posted earlier.
Think - the problem is that you're missing leading zeroes on values, so you need to supply those zeroes in the required cases before you print the values.
This isn't a programming problem, it is simple logic.

Got it... here's the code:

char foo[5] = {0x02,0x63,0x6e,0x00,0x00};

void setup(){

  Serial.begin(19200);  
  delay(3000);

  for ( int i=0 ; i<4 ; i++ ) 
     {
  if (foo [i] < 16)  {
     Serial.print ("0");
     }
     Serial.print(  foo[i], HEX ) ;
     Serial.print(" ");
     }
      Serial.println();
}

void loop(){
}

WE HAVE A WINNER!

:wink:

Another suggestion is to use auto-format (CTRL+T) to fix your indentation. Will make the code easier to read and debug, both for others and yourself.

I can't figure out why it's being drop.

Because it's useless. 02 and 2 are the same value. They are not the same string, but you aren't printing strings. You are printing values. If you want to print strings, or values as strings, you are going about it wrong. sprintf() bears looking at.