Hello there,

I got a problem with a simple function supposed to read a text file and get its content into a buffer.
The file (.txt) must contains HEX values, but when I put something like "60 84 33" the ReadFile API function converts it to another Hex value, as the original values were ASCII values.

But I want my function to take directly the HEX values in the .txt without modifying them.

Is there an API function for this?

How can I get the same value in the .txt and in my buffer WITHOUT using ASCII ? (i don't trust in ASCII... I want some good old hex val..)

Thanks in advance
Posted on 2006-11-08 06:49:05 by marc_
If you put "60 84 33" into a text file, it it treated as
  BYTE 36h,30h,20h,38h,34h,20h,33h,33h

so you have 2 options:
1. Convert the ASCII to hex bytes
2. Write the original file as binary
Posted on 2006-11-08 07:46:07 by sinsi
Ok, so that's what I was afraid of..

No option in ReadFile that woud make the conversion for me..

Do you know what's usually used to make this?
I thought Iczelion talked about it but couldn't find anything in his tuts.
Is there a famous function in asm to do it?
Or could you show me the way to find it?

Thanks helping me,

good night (midnight here in paris ^^)
Posted on 2006-11-08 16:49:56 by marc_
You should treat each of the bytes in the file as a hex value. A hex value in a byte is made of two nibbles each of which is made of 4 bits. So you have a character which is 8 bits or 2 nibbles or a single hex value. For example, the character '6' while placed inside a text file will not be 0x06 while read from the file. When you read the file, you read the ASCII value of the character '6' which is 48+6 or 54. You can try this out by holding down the ALT key and pressing 0054 in your keypad.

If you need to treat each character in the file as a HEX value, you should write a routine for it. Here is what you have to do:

1) Take the read character and treat it as a byte.
2) Subtract 48 from the byte.
3) See if the resulting byte is greater than 9. If yes, this means that you have to convert the value to A..F in HEX and not a digit.
4) Subtract another 7 from the number if the previous conditional statement was true. This will give us for example 'A'-55 = 65-55=10 which is the value of 'A' in HEX. You can also make an effort to convert all characters to uppercase using AND 0xCF to be able to handle lowercase and uppercase letters.

Below is the "HexToDWORD" function I have written in Delphi. I have tried breaking the dependence chains and avoided partial flag register stalls. The function works at 113 clock cycles on my PIII 800MHZ with a DWORD aligned buffer and stack pointer. The clock cycles which it takes to convert '0' in hex to DWORD is 74 which could be pretty faster if I had checked for the null-terminator at the second byte of the code.



Posted on 2006-11-09 07:47:20 by XCHG