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:
while (p < sizeof(input))
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.
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) %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:
input[length] = 0;
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.
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
Well, basically I have an array as such:
unsigned char input;
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?
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).