hexa to ascii char conversion

Jul 23, 2016 at 5:14pm
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
Jul 23, 2016 at 5:37pm
Hi,

So how are you trying to convert hex to ascii? Can you show us your whole code?
Jul 23, 2016 at 8:16pm
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)"

Jul 23, 2016 at 8:22pm
Try printf("%c ", DATA[i])
Jul 23, 2016 at 8:55pm
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?
Jul 24, 2016 at 4:00am
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?
Jul 24, 2016 at 6:02am
How about you use some standard libraries or write some function of your own?
Jul 24, 2016 at 6:50am
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 Jul 24, 2016 at 6:50am
Jul 24, 2016 at 8:19am
Try
1
2
3
4
5
#include <ctype.h>
if (isprint(DATA[i]))
   printf("%c ", DATA[i]);
else
   printf("%X ", DATA[i]);


Jul 24, 2016 at 8:33am
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
Jul 24, 2016 at 10:00am
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.