0

I am trying to convert the hexadecimal values of an array into characters of another one. Here is the only way, I could find but not working.

Thank you for your help !

char tmp[] = {0x81, 0x00, 0x00, 0x00, 0x12, 0x05};
char new[12];

for (int i = 0; i < 6; i++) {
    printf(" %x", tmp[i]);
    sprintf(new + i, "%x", tmp[i]);
}
for (int i = 0; i < 12; i++) {
        printf(" %c", new[i]);
}
printf("new: %s\n", new);

Here is the output :

 81 0 0 0 12 5
 8 0 0 0 1 5
new: 800015

So, it lacks some bytes ...

5
  • What do you mean "characters". The ASCII representation of hex values? But you only have 6 bytes so that won't fit. You are also using sprintf for writing into the same array as the target... Commented Feb 7, 2019 at 14:14
  • What output do you expect? Commented Feb 7, 2019 at 14:14
  • sprintf(tmp + i, "%x", tmp[i]); will lead to undefined behavior. You attempt to write a multi-character string into a single character. Not to mention that using tmp[i] as both an argument and as destination is also UB as per the C specification. Commented Feb 7, 2019 at 14:15
  • For binary to hex ASCII conversion, there are thousands of snippets already posted on the internet, such as: stackoverflow.com/a/54129120/584518 Commented Feb 7, 2019 at 14:16
  • Your edit doesn't change much: You're still writing a multi-character string into the single character new[i]. Also note that if char is signed (it may be signed or unsigned, it's an implementation (compiler) detail) then 0x81 would lead to sign extension as it's considered a negative value. Use uint8_t from <stdint.h> for generic unsigned bytes. And please learn about two's complement (which is the most common way to handle negative values). Commented Feb 7, 2019 at 14:21

1 Answer 1

3

probably

char tmp[] = {0x81, 0x00, 0x00, 0x00, 0x12, 0x05};
char new[6];

must be

int tmp[] = {0x81, 0x00, 0x00, 0x00, 0x12, 0x05};
char new[6*2+1];

and

sprintf(tmp + i, "%x", tmp[i]);

must be

sprintf(new + 2*i, "%02x", tmp[i]);

and

for (int i = 0; i < 6; i++) {
        printf(" %c", new[i]);
}

must be

for (int i = 0; i < 6*2; i++) {
        printf(" %c", new[i]);
}

Execution :

/tmp % ./a.out
 81 0 0 0 12 5 8 1 0 0 0 0 0 0 1 2 0 5new: 810000001205

Under valgrind :

/tmp % valgrind ./a.out
==15557== Memcheck, a memory error detector
==15557== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==15557== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==15557== Command: ./a.out
==15557== 
 81 0 0 0 12 5 8 1 0 0 0 0 0 0 1 2 0 5new: 810000001205
==15557== 
==15557== HEAP SUMMARY:
==15557==     in use at exit: 0 bytes in 0 blocks
==15557==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==15557== 
==15557== All heap blocks were freed -- no leaks are possible
==15557== 
==15557== For counts of detected and suppressed errors, rerun with: -v
==15557== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)
Sign up to request clarification or add additional context in comments.

6 Comments

@user694733 you are right, I see after, I edited my answer
There's still IMO no reason to use such a cumbersome function as sprintf for something fairly trivial like this.
@Lundin lol I agree, I just corrected the errors from the original definition ^^
@Lundin How you do without sprintf ?
@GaelG I posted a link to a snippet as comment below your question.
|

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.