hexa to ascii char conversion

Hi,

I have read in a binary file to an array of chars (DATA). So far so good however I cannot convert it to "human readable format" since it seems to be in hexadecimal format. I know that the file has a 512 byte long header and in matlab float32 is the type in the fread function. The rest of the file is the data in double or float (hopefully).

When I try to print it out then the std::cout << DATA[i] << "\n" does not print anything. I can access the hexa format easily with a c-type printf("%X ", DATA[i]) funtion. However this gives an error: printf("%s ", DATA[i])

Is there a way in C++ to convert the hexadecimal information to "human readable format" (ASCII)?
How can I print the hexadecimal info with cout?
Why do I get different entries if I use unsigned char rather then char for the DATA type?

Thanks,
Z
Hi,

So how are you trying to convert hex to ascii? Can you show us your whole code?
Yeah so typical entries are after printing it with printf("%X ", DATA[i]):
FFFFFFEF
FFFFFFEE
6E
4F
7B
14
16
40

So far I tried this:
printf("%f \n",(float)(char)DATA[i]);

This gives numbers but they are too small and probably wrong. I do not want to use the printf function so I tried the stof

float y = 0;
y = stof(DATA[i]);

But this gave the error of:
"terminate called after throwing an instance of 'std::invalid_argument'
what(): stof
Aborted (core dumped)"

Try printf("%c ", DATA[i])
printf("%c ", DATA[i])

gave some uninterpretable print out on the console.

Is it a problem that the 0x is missing from the beginning of the entries?
closed account (48T7M4Gy)
It seems you are assuming there is a one-to-one correspondence with the data in the binary file and the ASCII conversion. Maybe it's not that simple with the binary file and the data is altered in som way and otherwise surrounded by other non-data information. It might be executable code?
How about you use some standard libraries or write some function of your own?
closed account (48bpfSEw)
 
printf("%f \n",(float)(char)DATA[i]);



I think the problem is that:

First DATA[i] is converted into ONE character
then this single character is converted into float
BUT float needs 4 bytes!

May be this could work:

1
2
3
void* p = &DATA[i]
float* pf = (float *) p;
std::cout << *pf << std::endl;

In a single line:

 
std::cout << *((float*)(void*)(&DATA[i])) << std::endl;


Last edited on
Try
1
2
3
4
5
#include <ctype.h>
if (isprint(DATA[i]))
   printf("%c ", DATA[i]);
else
   printf("%X ", DATA[i]);


The file is definitely not an executable. It is a data file for sure. It contains a whole loads of floats. However how many of these floats are stored is coded in the header part of the binary. The final goal would be to write my own function of course but before that I would like to understand the nitty-gritty details. I think where I went wrong so far is what Necip points out that I need actually 4 bytes for the float. However I thought it is possible to print it as an ascii human readable file and then see what is what. Well, I was wrong there. :)

Thanks,
Z
Hi,

Thanks all for the answers and it seems this not as straightforward problem as I thought. I will have to research this a bit more.

Thanks,
Z
Topic archived. No new replies allowed.