Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary (hex) files and converting hex to ascii characters

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello
One of the things that I have to do is to read a binery file containing messages.
I have to read one bit(character) at the time, so I can compare it to my message key. If my message key is 15 then I have to search the file until I find the hex bit that is the number 15. After I have found the 15 which means that my message is starting than I have to take all the bits after that one until I find another bit say he 0x3 that menas end of message. I have to take all the bits (hex) and convert it to ascii so I can display my message. After that I still need to continue to do the same thing and check for my messages until the file end.
(what I'm trying to do is to simulate the data-link layer).

Thank you
 
Hi,

confusion !!

I belive that your "bit" is byte ( 8 bit ), right ?

Computer files are all binary object, memory areas too.

We call text-files, those that contains bytes in the range

from 32 decimal ( the space char in ascii rappr. )
to 126 decimal ( the tilde char, more or less ).

If a file contains for example 4 bytes:

physically in bit

01000000 01000001 01000010 00001010

I can intepreter them as:

a text file with 3 bytes of text

decimal
64 65 66 10

ascii
@ A B newline

or for me they can be 2 16-bit words

16449 16906

or 1 32 bit word (longword)

1078018570

Then, the files contain bits, when I print on paper their
value, I can choice to print the same value in:

binary ( rare )
decimal ( often )
hex ( sometime )
ascii rap ( often )

Suppose :

char c=65 ;

printf( "%d", c ) -----> 65
printf( "%c", c ) -----> A
printf( "%x", c ) -----> 41
printf( "%o", c ) -----> 101

- - - - - - - - - - -

Back to your problem.

Has your file a fixed record len ? ( the messages have all
the same lenght ? )

Your binary file containing a 0x03 means that

1) I have a bit 00000011
or
2) a text file with written inside 0x03 in text
or
3) other ?.

I first case you have to use _read function (or ReadFile)

and rc=_read( myhandle, mybuf, 1 ) // if the len is 1 byte

int len = mybuf[0] ;

rc=_read( myhandle, mybuf, len )

If the len on file is a word, you can

WORD wLen;
rc= _read( myhandle, &wLen, 2 ) ;

Be careful in binary rappr can be swapped from a processor
to another ( little or big indian )


bye .



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top