convert byte array into string

Discussion in 'C Programming' started by alessio211734, Feb 5, 2009.

  1. 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.
    alessio211734, Feb 5, 2009
    #1
    1. Advertising

  2. alessio211734

    James Kuyper Guest

    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.
    James Kuyper, Feb 5, 2009
    #2
    1. Advertising

  3. On 5 Feb, 15:57, James Kuyper <> 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"
    alessio211734, Feb 5, 2009
    #3
  4. alessio211734 <> 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);

    --
    Ben.
    Ben Bacarisse, Feb 5, 2009
    #4
  5. alessio211734

    Lew Pitcher Guest

    On February 5, 2009 10:58, in comp.lang.c, alessio211734
    () 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. ------
    Lew Pitcher, Feb 5, 2009
    #5
  6. alessio211734

    Bart Guest

    On Feb 5, 3:58 pm, alessio211734 <> wrote:
    > On 5 Feb, 15:57, James Kuyper <> 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
    Bart, Feb 5, 2009
    #6
  7. 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]);
    }
    J. J. Farrell, Feb 6, 2009
    #7
  8. alessio211734

    CBFalconer Guest

    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.
    CBFalconer, Feb 7, 2009
    #8
  9. alessio211734

    CBFalconer Guest

    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.
    CBFalconer, Feb 7, 2009
    #9
  10. 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.
    J. J. Farrell, Feb 8, 2009
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kirby
    Replies:
    3
    Views:
    612
    Kirby
    Oct 8, 2004
  2. Replies:
    20
    Views:
    9,677
    licebmi
    Sep 7, 2009
  3. Polaris431
    Replies:
    8
    Views:
    730
    SM Ryan
    Dec 4, 2006
  4. Replies:
    1
    Views:
    462
    Thomas J. Gritzan
    Jul 20, 2006
  5. Replies:
    4
    Views:
    1,341
Loading...

Share This Page