sprintf padding hex with an extra 0

Hopefully this is a straightforward issue:

I have an int. I want to convert it to a char array and represent it as a hex, so this is what I do:

sprintf(computedsum,"%X", sum);

If the number is, say, 5A in hex, the output then reads "05A"

How do I get rid of the 0?

If you post more than this line of code, we'd be able to help.

How is computedsum defined? How is it printed?

Thanks for the reply. Here’s more of the code, it’s essentially a checksum function, and computedsum is defined in the first line of the function

char *checksum(char * GPSdata, char computedsum[5])
{
  sum = 0;
  int j = 0;
  while (j < i)
  {
    byte data = GPSdata[j];
    switch (data)
    {
    case '

It’s printed using Serial.print(computedsum):
      break;
    case ‘*’:
      j = i;
      break; //this indicates the end of the sentence
    default:
      if (sum == 0)
      {
        sum = GPSdata[1];
      }
      else
      {
        sum ^= data;
      }
    }
    j++;
  }
  sprintf(computedsum,"%*X", 2, sum);
  return computedsum;
}


It's printed using Serial.print(computedsum)

Why are you returning an input argument?

The sprintf call in that code does not look like the one you posted initially.

My book on printf does not define the meaning of the * between the % and the X. Perhaps it’s presence is related to the extra 0 that you get.

Right, I just posted code without going it over. That * was one of the things I was trying to use to get rid of the 0.

I'm not very good with functions and returning things.... do I not need to return computedsum after I use sprintf?

sprintf(computedsum,"%*X", 2, sum);

The meaning of the * is that it'll take the padding from the next argument, "2". So that's equivalent to

sprintf(computedsum,"%2X", sum);

That padding, however, only refers to spaces, not zeroes.

But you never showed us of which type "sum" is, which probably affects the precision of the output.

Anyway, a valid fix would be

sprintf(computedsum,"%.2X", sum);

Which will by default use 2 digits, but more if more are required. (It'll also use 2 digits if only 1 is needed.)

Oh sorry, I thought I had initialized sum within that function. It’s an int.

Your proposed solution doesn’t work, it just gives the same issue. I’m sorry I really should have mentioned at first that I’m using a chipkit board not an arduino per se.

It’s really not that big of a deal, I’ll just pad the reported checksum from the GPS device with a 0 in front and then I should be able to compare the two, which is what I’ve been trying to do.

Thanks for the help.

If it is still doing that, the zero is likely to come from somewhere else and not the sprintf.

Without seeing all the code it is very difficult to tell what is happening.
There are several declarations that we cannot see and we cannot see how
this function is being called.
The declaration for the computedsum argument in the checksum() routine
looks suspicious. While you can specify an array that way, I believe that it
is no different than using “char *” which will not allocate any storage for the variable
since it is only a pointer.

Also, are you sure that the output was “05A” and not “05A”, if there is a space
there, it could be that you are printing 2 checksums back to back and the first is a 0 which will
print “0” and not “00”.

Just a guess, but as far as the actual format strings goes, perhaps you want “%02X” which
will give a zero filled 2 digit hex number. So that if you have numbers less than 0x10
they will get a ‘0’ filler character rather than a space.
i.e. 0x3 will display as ‘03’ rather than 3 and 0x0
will display as ‘00’ rather than 0

The “" field width option as a format string to xxprintf() functions, while
useful, is not always supported.
While I’ve used it in the past, I’m surprised that is working for you guys,
as it doesn’t seem to work with my version of the AVR toolset that came with with 0022 IDE
for linux. When I use "
” in the formatting string, all the output is dropped.
(but that is a different problem to worry about later…)

Can you boil this down to a small complete program that compiles?
That would be helpful.

— bill