8

Hello I am using digi dynamic c. I am trying to convert this in to string

char readingreg[4];
readingreg[0] = 4a;
readingreg[1] = aa;
readingreg[2] = aa;
readingreg[3] = a0;

Currently when I do printf statements it has to be like this:

printf("This is element 0: %x\n", readingreg[0]);

But I want this in string so I can use printf statement like this

  printf("This is element 0: %s\n", readingreg[0]);

I am essentialy sending the readingreg array over TCP/IP Port, for which I need to have it as string. I cant seem to be able to convert it into string. Thanks for your help. Also if someone can tell me how to do each element at a time rather than whole array, that would be fine to since there will only be 4 elements.

M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
Dale Reed
  • 93
  • 1
  • 2
  • 4
  • Please clarify, what exactly to you want the second printf() to print. – James Curran Sep 02 '14 at 19:42
  • [inet_ntoa()](http://linux.die.net/man/3/inet_ntoa) – Ryan Haining Sep 02 '14 at 19:52
  • Use `snprintf` to print to a buffer, then print that? Also remember the string terminating 0 byte. – hyde Sep 02 '14 at 19:52
  • Looking at answers i dont think i was clear, sorry. So essentialy i have an array which is readingreg (char) but has hex values. I want its values to be string. For example: readingreg[0] = 4a, 4a is hex value can someone help me in making a new array and it would look like: newArray[0] = 4a; newArray[1] = aa; and so on where 4a and aa will be strings rather than hex. – Dale Reed Sep 02 '14 at 20:17

2 Answers2

9

0xaa overflows when plain char is signed, use unsigned char:

#include <stdio.h>

int main(void)
{
    unsigned char readingreg[4];
    readingreg[0] = 0x4a;
    readingreg[1] = 0xaa;
    readingreg[2] = 0xaa;
    readingreg[3] = 0xa0;
    char temp[4];

    snprintf(temp, sizeof temp, "%x", readingreg[0]);
    printf("This is element 0: %s\n", temp);
    return 0;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • This gives an error. `pointer targets in passing argument 1 of ‘sprintf’ differ in signedness [-Werror=pointer-sign]`. Probably it is a warning but I have `Werror` on. – Duck Dodgers Jan 28 '21 at 09:06
  • Are you sure? there are not pointers involved in this code and I'm compiling with stricts warnings (`-Wpedantic -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wconversion -Wshadow -Wcast-qual -Wnested-externs`) and `gcc` compiles silently without warnings this piece of code. Same for godbolt: https://godbolt.org/z/9h7rEP no warnings. Which compiler are you using? – David Ranieri Jan 28 '21 at 09:19
  • If I change the data type of `temp` to `char`, then I do not get any errors. I have gcc8.3.0 on Debian 10. Thank you for the prompt update. The `unsigned char` data type is important only for the variable storing the byte-array, where using a `char` could cause an overflow. – Duck Dodgers Jan 28 '21 at 09:43
  • You are welcome, _I have gcc8.3.0 on Debian 10_ - that's odd, I'm also on Debian 10 and I don't receive any warning, same on my laptop with Ubuntu 18.04 – David Ranieri Jan 28 '21 at 09:52
  • @DavidRanieri My bad I misread the intent of the code. I'll delete my comments soon to keep this clean. – Chnossos Apr 06 '21 at 12:44
2

If your machine is big endian, you can do the following:

char str[9];

sprintf(str, "%x", *(uint32_t *)readingreg);

If your machine is little endian you'll have to swap the byte order:

char str[9];
uint32_t host;

host = htonl(*(uint32_t *)readingreg);
sprintf(str, "%x", host);

If portability is a concern, you should use method two regardless of your endianness.

I get the following output:

printf("0x%s\n", str);

0x4aaaaaa0

Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46