Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting hex to string in C?

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.

like image 672
Dale Reed Avatar asked Sep 02 '14 19:09

Dale Reed


People also ask

How do you convert hex to string?

In order to convert a hex string into a normal string, the hex string has to be converted into a byte array, which is indexed and converted into smaller hex strings of two digits. The smaller hex strings are then concatenated into a normal string. For some values, a two digit hex string will start with a zero.

Does Atoi work on hex?

Does atoi work on hex? atoi(s[,base]) converts a string into an integer. The default is decimal, but you can specify octal 8, hexadecimal 16, or decimal 10.

How can I convert a hex string to an integer value?

To convert a hexadecimal string to a numberUse the ToInt32(String, Int32) method to convert the number expressed in base-16 to an integer. The first argument of the ToInt32(String, Int32) method is the string to convert. The second argument describes what base the number is expressed in; hexadecimal is base 16.

What is hex encoded string?

Hex encoding is performed by converting the 8 bit data to 2 hex characters. The hex characters are then stored as the two byte string representation of the characters. Often, some kind of separator is used to make the encoded data easier for human reading.


2 Answers

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];

    sprintf(temp, "%x", readingreg[0]);
    printf("This is element 0: %s\n", temp);
    return 0;
}
like image 112
David Ranieri Avatar answered Sep 28 '22 04:09

David Ranieri


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

like image 22
Fiddling Bits Avatar answered Sep 28 '22 04:09

Fiddling Bits