Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > convert byte array into string

Reply
Thread Tools

convert byte array into string

 
 
alessio211734
Guest
Posts: n/a
 
      02-05-2009
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.
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      02-05-2009
alessio211734 wrote:
> 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.
 
Reply With Quote
 
 
 
 
alessio211734
Guest
Posts: n/a
 
      02-05-2009
On 5 Feb, 15:57, James Kuyper <(E-Mail Removed)> wrote:
> alessio211734 wrote:
> > 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"



 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      02-05-2009
alessio211734 <(E-Mail Removed)> writes:
<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"


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[i]);

--
Ben.
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      02-05-2009
On February 5, 2009 10:58, in comp.lang.c, alessio211734
((E-Mail Removed)) 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. ------


 
Reply With Quote
 
Bart
Guest
Posts: n/a
 
      02-05-2009
On Feb 5, 3:58*pm, alessio211734 <(E-Mail Removed)> wrote:
> On 5 Feb, 15:57, James Kuyper <(E-Mail Removed)> wrote:
>
>
>
>
>
> > alessio211734 wrote:
> > > 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"- 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.

--
Bartc
 
Reply With Quote
 
J. J. Farrell
Guest
Posts: n/a
 
      02-06-2009
Anthony Fremont wrote:
> 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]);
}
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-07-2009
Anthony Fremont wrote:
> 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.)

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-07-2009
CBFalconer wrote:
> Anthony Fremont wrote:
>> 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.

>
> 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.)


--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
J. J. Farrell
Guest
Posts: n/a
 
      02-08-2009
Anthony Fremont wrote:
> J. J. Farrell wrote:
>> Anthony Fremont wrote:
>>> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert form byte[4] to Int32 while retaining the binary value of the byte array jeff@foundrymusic.com C++ 20 09-07-2009 08:54 PM
How to convert a 4 byte character string to its equivalent 4 byte integer value? Polaris431 C Programming 8 12-04-2006 07:00 AM
how to convert byte array into integer msosno01@gmail.com C++ 4 07-24-2006 04:00 PM
how to convert byte array into integer msosno01@gmail.com C++ 1 07-20-2006 10:53 PM
Converting a Primative byte array to a Byte array object Kirby Java 3 10-08-2004 03:01 AM



Advertisments