0

I have an int value in the input and I want to save it to a buffer/array as a hexadecimal representation of ASCII. I know how to print it in hex form but how can I save it like this to the buffer?

Here's the code I wrote:

int a = 98765;
char buffer[20];
m = sprintf(buffer, "%d", a);

printf("ASCII value in hex: ");
for(int i=0; buffer[i]!='\0'; i++)
{
    printf("%02X", buffer[i]);
}

for eg if a=123 I want to get 313233

5
  • 2
    Why sprintf it in decimal in the first place? Why not just sprintf(buffer, "%02X". a)? Commented Oct 22, 2018 at 22:08
  • @LeeDanielCrocker thank you! I forgot to add that I want to get ASCII values in hex, not the whole number, so for example for a=123456 I want to get 313233343536 Commented Oct 22, 2018 at 22:15
  • I'm confused. From your example, it looks like your code should work as is. So, for 123, if you're not getting 313233 for output, what are you getting instead? I just downloaded and tested your code and it works, so double confused :-) Commented Oct 22, 2018 at 23:03
  • I just reread your question, so if you want to save what your printf prints into a buffer, you need a second buffer (e.g. char hexbuffer[50]). Now, replace your printf with: sprintf(&hexbuffer[i * 2],"%02X",buffer[i]). You can't do it in a single buffer because you're outputting twice as many chars Commented Oct 22, 2018 at 23:13
  • 1
    Curious why 20 in char buffer[20];? Note: had a been 64-bit, 21 may be needed for sprintf(buffer, "%d", a);. Commented Oct 22, 2018 at 23:39

2 Answers 2

3

The first sprintf gave you an array of char, where each one is in the range 0x30 to 0x39. You want those to be expanded to the two characters '3' and '0', '3' and '1', etc.

You're expanding each char in the source to two chars in the output, which means you can't use the same destination buffer as source buffer -- you'd overwrite values in the source buffer before you processed them. You probably want to do the first sprintf into a separate array of char if you need the result in buffer. In your loop, change the printf to a sprintf to an index in buffer, incrementing your index by two each loop. (By two because that way you'll be overwriting the first iteration's string terminator with the second iteration's sprintf output, which will include a new string terminator. The sprintf in the last iteration of the loop will terminate the string for you.)

You didn't mention the longest input you're supposed to handle, or why you chose the array lengths you did. There's a danger of buffer overruns here if you accept inputs that are too long for your buffers (and don't forget to account for space for the null termination after the last character.)

Sign up to request clarification or add additional context in comments.

6 Comments

Given that he already has the binary value of each character due to sprintf, this is just another method of doing what he has already done. It does not answer the question which is about storing the text in an array rather then printing it.
I'll rewrite. They've rewritten the question.
Ah - yes the change to the question is a significant clarification. I can see how you might have answered thus. The edit however is older than your answer (albeit by a number of minutes). All can be edited in SO; it is both a benefit and a curse, as it can invalidate existing answers.
Clifford: I rewrote my comment before your reply because I saw that it was unreasonable. I apologize for making baseless accusations and being off topic. I've also rewritten the answer in response to your critique.
It is good to point out the danger of buffer overrun, but if an int is 32 bit, then it can have no more that ten digits, so the hex expansion would be limited to 20 characters. There is no danger of the user input causing a buffer overrun in this case - the buffers simply need to be large enough but are deterministic.
|
1

You have already used sprintf to represent decimal values as a string; you can do the same for hexadecimal formatting.

Use the return value of sprintf() (which is the number of characters 'printed') to advance an index into the buffer as the destination for the next hex-digit pair, then iterate each character in the decimal buffer.

Note that, you can use the return value (m) from the first sprintf() for the iteration rather then testing for nul.

char hexbuffer[64];
int index = 0 ;
for(int i = 0; i < m; i++)
{
    index += sprintf( &hexbuffer[index], "%02X", buffer[i] ) ;
}

Of course if the input includes only decimal digits (i.e. the user does not enter a negative value causing a - at buffer[0], you could simply insert a 3 ahead of each decimal digit, thus:

char hexbuffer[64];
for(int i = 0; i < m; i++)
{
    bexbuffer[i * 2] = '3' ;
    hexbuffer[i * 2 + 1] = buffer[i] 
}

If only decimal digits were intended, you should change the type of a to unsigned and use the %u format specifier for the decimal input.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.