convert byte array into string

A

alessio211734

Using c language what's the best way to convert a byte array as char
*

An example:

byte array[]={10, 255, 10}

I want convert it in this way in char*

"0A FF 0A" in hex.


Thanks in advance.
 
J

James Kuyper

alessio211734 said:
Using c language what's the best way to convert a byte array as char
*

An example:

byte array[]={10, 255, 10}

There is no standard C type or typedef known as byte. The most
reasonable definition would be:

typedef unsigned char byte;

I'll assume that's what you're using, but it would be better to tell us
what definition you're using.
I want convert it in this way in char*

Well, it's an array, you can't convert into a char*. You could
reinterpret it as a char*, but it's the wrong length for that purpose on
most of the machines I've ever used. You can form a char* which points
at the first element of that array using the expression

(char*)array
"0A FF 0A" in hex.

If 'byte' is defined as I suggested above, then a hex dump of array
would show exactly what you've given; no conversion is necessary. If it
isn't, I'm not sure what you're asking for.
 
A

alessio211734

alessio211734 said:
Using c language what's the best way to convert a byte array as char
*
An example:
 byte array[]={10, 255, 10}

There is no standard C type or typedef known as byte. The most
reasonable definition would be:

        typedef unsigned char byte;

I'll assume that's what you're using, but it would be better to tell us
what definition you're using.
I want convert it in this way in char*

Well, it's an array, you can't convert into a char*. You could
reinterpret it as a char*, but it's the wrong length for that purpose on
most of the machines I've ever used. You can form a char* which points
at the first element of that array using the expression

        (char*)array
"0A FF 0A" in hex.

If 'byte' is defined as I suggested above, then a hex dump of array
would show exactly what you've given; no conversion is necessary. If it
isn't, I'm not sure what you're asking for.


I want a array of unsigned byte as you tell is show as a string of hex
values.
unsigned char msg[10]={10,11,12,255} should be show as a char msgresult
[200]="0A 0B 0C 255"
 
B

Ben Bacarisse

alessio211734 said:
I want a array of unsigned byte as you tell is show as a string of hex
values.
unsigned char msg[10]={10,11,12,255} should be show as a char msgresult
[200]="0A 0B 0C 255"

The gist of what you want is the %x format. Together with the '0'
modifier and a length of 2 you get what you need:

int i;
unsigned char msg[10] = {10,11,12,255};
for (i = 0; i < sizeof msg; i++)
printf("%02x ", msg);
 
L

Lew Pitcher

On February 5, 2009 10:58, in comp.lang.c, alessio211734
([email protected]) wrote:
[snip]
I want a array of unsigned byte as you tell is show as a string of hex
values.
unsigned char msg[10]={10,11,12,255} should be show as a char msgresult
[200]="0A 0B 0C 255"

OK. There is no "easy, one step" way to do this in standard C (there /may/
be 3rd-party extensions that do this, but I don't know what they are).

However, this is a very simple matter of programming, easily accomplished
with standard C tools. And, any programmer beyond "casual newbie" level
should be able to figure this out without help. I'll even give you some
hints:
1) Tell me how you would convert /one/ char value to a hex string
representation.
2) Tell me how you would determine the number of char values in your array,
3) Combine these two things together, with whatever 'glue' code you need, to
create a hex representational string of the multiple char values found in
the array.
4) you are done.

I'll even tell you some standard C operators and functions that will help:
sizeof to determine the size (if not already known) of your array
for () { } to iterate through your array

sprintf() to generate a hex representation string from one char value
strcat() to concatinate strings together
malloc() to allocate a working buffer for strings and such
printf() to print your hex representational string (optional)

Now, show us your code.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
B

Bart

alessio211734 said:
Using c language what's the best way to convert a byte array as char
*
An example:
 byte array[]={10, 255, 10}
There is no standard C type or typedef known as byte. The most
reasonable definition would be:
        typedef unsigned char byte;
I'll assume that's what you're using, but it would be better to tell us
what definition you're using.
Well, it's an array, you can't convert into a char*. You could
reinterpret it as a char*, but it's the wrong length for that purpose on
most of the machines I've ever used. You can form a char* which points
at the first element of that array using the expression
        (char*)array
If 'byte' is defined as I suggested above, then a hex dump of array
would show exactly what you've given; no conversion is necessary. If it
isn't, I'm not sure what you're asking for.

I want a array of unsigned byte as you tell is show as a string of hex
values.
unsigned char msg[10]={10,11,12,255} should be show as a char msgresult
[200]="0A 0B 0C 255"- Hide quoted text -

Well this converts one byte to a string:

char s[100];
sprintf(s,"%02X ",0x0A);

But you need to add extra logic if you want 255 to appear as 255
rather than FF.
 
J

J. J. Farrell

Anthony said:
alessio211734 said:
Using c language what's the best way to convert a byte array as char
*

An example:

byte array[]={10, 255, 10}

I want convert it in this way in char*

"0A FF 0A" in hex.

Here's one way to do it: (UART_PutChar is my own function to send one hex
character (0-F) to the serial port of an embedded system)

void print_hex(unsigned char c) {

unsigned char i;

i = c>>4;
if(i>9)
i=i+'A'-10;
else
i=i+'0';
UART_PutChar(i);

i = c & 0x0f;
if(i>9)
i=i+'A'-10;
else
i=i+'0';
UART_PutChar(i);
}

That will only work correctly for some character sets; C doesn't
guarantee that the characters A to F are consecutive in numerical value.
An alternative which will work with all character sets is

void print_hex(unsigned char c) {

const unsigned char hexdigits[] = "0123456789ABCDEF";

UART_PutChar(hexdigits[c >> 4]);
UART_PutChar(hexdigits[c & 0xf]);
}
 
C

CBFalconer

Anthony said:
J. J. Farrell wrote:
.... snip ...
That will only work correctly for some character sets; C doesn't
guarantee that the characters A to F are consecutive in numerical
value. An alternative which will work with all character sets is

void print_hex(unsigned char c) {

const unsigned char hexdigits[] = "0123456789ABCDEF";

UART_PutChar(hexdigits[c >> 4]);
UART_PutChar(hexdigits[c & 0xf]);
}

Yes, that's very nice code. Can you identify one character set
where the code I posted won't work?

You are missing the point. Mr. Farrell is using code that does not
depend on unguaranteed characteristics. So it will always work
(apart from the assumption of CHAR_BIT == 4). The moment you worry
about 'identifying character sets' you abandon that assurance.

Such things are the purpose of the C standard. It guarantees that
code that matches it will, when compiled and run on compliant
systems, work. The trolls we have here attempt to confuse that
assurance.

(Incidentally, adding 'static' to the definition of hexdigits
should help efficiency.)
 
C

CBFalconer

CBFalconer said:
Anthony said:
J. J. Farrell wrote:
... snip ...
That will only work correctly for some character sets; C doesn't
guarantee that the characters A to F are consecutive in numerical
value. An alternative which will work with all character sets is

void print_hex(unsigned char c) {

const unsigned char hexdigits[] = "0123456789ABCDEF";

UART_PutChar(hexdigits[c >> 4]);
UART_PutChar(hexdigits[c & 0xf]);
}

Yes, that's very nice code. Can you identify one character set
where the code I posted won't work?

You are missing the point. Mr. Farrell is using code that does not
depend on unguaranteed characteristics. So it will always work
(apart from the assumption of CHAR_BIT == 4). The moment you worry
about 'identifying character sets' you abandon that assurance.

That SHOULD be CHAR_BIT == 8.
 
J

J. J. Farrell

Anthony said:
J. J. Farrell said:
Anthony said:
alessio211734 wrote:
Using c language what's the best way to convert a byte array as char
*

An example:

byte array[]={10, 255, 10}

I want convert it in this way in char*

"0A FF 0A" in hex.
Here's one way to do it: (UART_PutChar is my own function to send
one hex character (0-F) to the serial port of an embedded system)

void print_hex(unsigned char c) {

unsigned char i;

i = c>>4;
if(i>9)
i=i+'A'-10;
else
i=i+'0';
UART_PutChar(i);

i = c & 0x0f;
if(i>9)
i=i+'A'-10;
else
i=i+'0';
UART_PutChar(i);
}
That will only work correctly for some character sets; C doesn't
guarantee that the characters A to F are consecutive in numerical
value. An alternative which will work with all character sets is

void print_hex(unsigned char c) {

const unsigned char hexdigits[] = "0123456789ABCDEF";

UART_PutChar(hexdigits[c >> 4]);
UART_PutChar(hexdigits[c & 0xf]);
}

Yes, that's very nice code. Can you identify one character set where the
code I posted won't work?

Not off-hand. I was thinking of EBCDIC, but on checking I see that A to
F are sequential even though there are breaks elsewhere in the A to Z
sequence. However, C doesn't guarantee it - just accepting that saves
you having to know anything about character sets, particularly since
it's easy to write code which doesn't depend on the assumption.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top