1

Let us say some file "file.txt" has the contents

56
21
f6
ad

the contents of the above file are in hexadecimal when i am reading them from the file and stored them in a buffer "arr" in the below code Now when I try to read from the buffer "arr" the values stored as char which should be expected. Is there any way I can read hexadecimal values directly in the buffer?

my intention is to read 5 as 5(base 10) ,read 6 as 6(base 10) ,read 2 as 2(base 10) ,read 1 as 1(base 10) ,read f as 15(base 10) ,read 6 as 6(base 10) ,read a as 10(base 10) ,read d as 14(base 10) one by one . Is there any way to do that?

%include "io.mac"
    
        .DATA
                File_name       db      "file.txt",0
        .UDATA
                file_pointer    resd    1
                arr             resb    4
        .CODE
        .STARTUP
                mov         EAX,5           ;opening the file "file.txt"
                mov         EBX,File_name
                mov         ECX,0
                mov         EDX,0o777
                int         0x80
                mov         [file_pointer],EAX
        
                mov         EAX,3           ;reading from the file "file.txt"
                mov         EBX,[file_pointer]
                mov         ECX,arr
                mov         EDX,4
                int         0x80
    .END
Mike
  • 9
  • 3
  • What assembler are you using? It looks like NASM syntax, but `.DATA` and `.CODE` look like MASM directives. – Peter Cordes Apr 16 '22 at 08:33
  • Anyway, bytes in files and in memory are just raw bytes. They don't have a type. `0x56` is the ASCII code for capital `V`, so if you write that byte to a terminal, that's the character it will display. If you want to convert that 8-bit integer to a 2-digit string of hex digits that represent its value, see [How to convert a binary integer number to a hex string?](https://stackoverflow.com/q/53823756) – Peter Cordes Apr 16 '22 at 08:35
  • Or do you mean that you have a text file with those ASCII digits, so 3 bytes per line including the newline, and you want to do hex-string -> binary? You can of course do that yourself, but there's no system call that will do it for you. There are library calls like `scanf` or `strtol` that will do string->integer conversion with your specified base. – Peter Cordes Apr 16 '22 at 08:37
  • If the latter, near duplicate of [Converting ASCII hex number to 32-bit binary integer in x86](https://stackoverflow.com/q/12900846) although that looks over-complicated so it's not actually a *good* example of how to do this. There's also [How do I convert a string representing a signed hex int into its signed int Doubleword number in 80x86?](https://stackoverflow.com/q/16047113) – Peter Cordes Apr 16 '22 at 09:15
  • @PeterCordes Its NASM, my intention is to read 5 as 5(base 10) ,read 6 as 6(base 10) ,read 2 as 2(base 10) ,read 1 as 1(base 10) ,read f as 15(base 10) ,read 6 as 6(base 10) ,read a as 10(base 10) ,read d as 14(base 10) **one by one** . Is there any way to do that? – Mike Apr 16 '22 at 09:23
  • Oh, so no hex involved at all. Yeah, that's easy, just subtract `'0'` from bytes that are in the `'0'..'9'` range, as in [NASM Assembly convert input to integer?](https://stackoverflow.com/a/49548057) but without the part that multiplies by 10 and adds. The range-check is just a sub/cmp/jbe or ja as seen in my answer there. (Like you'd use for [checking if a number is alphabetic](https://stackoverflow.com/questions/54536362/what-is-the-idea-behind-32-that-converts-lowercase-letters-to-upper-and-vice/54585515#54585515) or [in general](https://stackoverflow.com/a/5197264/224132)) – Peter Cordes Apr 16 '22 at 10:24
  • Or, easy for the 0..9 digits. Sorry, missed that you did eventually have a digit >= 10 in there, so yeah, same deal as in the earlier linked questions, check for `> '9'` and adjust to get the right number for characters in the `a..f` range. If you can assume every byte other than newline is a valid hexadecimal character, it can be done very efficiently. – Peter Cordes Apr 16 '22 at 10:29

0 Answers0