CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2008
    Posts
    8

    Exclamation How do I print out a byte array as a string?

    Hey guys,
    I am having problems printing out the string conversion value for an array of bytes. How do I do this?

    My code so far looks like this:
    int p=0;
    while (p < sizeof(input))
    {
    printf("%s",(char)input[p]);
    p++;
    }
    where input is my array of bytes in hex. Now, I needed to print out the ASCII equivalent of the bytes in the array.
    Thanks in advance for any help.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How do I print out a byte array as a string?

    I see two problems with that code:
    1) Don't trust sizeof() to give you array lengths----that will never work with dynamic-sized arrays, or arrays that have been passed into a function, so it's just a bad habit to get into. Pass a length parameter around with every array or use a std::vector.
    2) &#37;s corresponds to a char*. For a char, use %c.

    Additionally, if you can guarantee that the array has capacity one larger than the current length, you can do:
    Code:
    input[length] = 0;
    printf("%s",(char*)input);
    to get the same result. Your method will only produce different results than this one if one of the bytes in input (between 0 and length-1) is 0.

  3. #3
    Join Date
    Nov 2008
    Posts
    8

    Question Re: How do I print out a byte array as a string?

    Code:
    input[length] = 0;
    printf("%s",(char*)input);
    thanks for the help lindley but the code above is printing out the hex values of what is in the array whereas I have an array of hex values and I am trying to print out the ASCII equivalent. my array is stored as an unsigned char array

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How do I print out a byte array as a string?

    You're going to have to give more context. There's no reason that would print out hex values.

  5. #5
    Join Date
    Nov 2008
    Posts
    8

    Question Re: How do I print out a byte array as a string?

    Well, basically I have an array as such:
    unsigned char input[64];
    and I am reading in lines of 64 bytes from a file and storing the line in input using fread. Now, I want to print out what is stored in input to stdout. The text in the file is is to be deciphered and printed in ASCII. How would I go about doing this?

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How do I print out a byte array as a string?

    I'm not entirely clear what the encoding of the file is, because that's necessary to answer the question. If you simply cast input to char* and output it, then the bytes will be interpreted as if the file already contained ASCII characters. If the file contains something else, you need to be more specific about what it is (perhaps even an example or two).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured