Tsukihime Posted May 11, 2011 Share Posted May 11, 2011 (edited) This is my input text file "hello.txt" in hex: 00 00 68 65 6C 6C 6F Which is basically "hello" with two null bytes before it. I want to read 7 bytes starting from the beginning of the file then print out the results, so I would expect to get a couple null bytes and the word "hello" I tried $file = FileOpen("hello.txt", 1) $line = FileRead($file, 7) ConsoleWrite($line) But nothing was printed out. It seems like the null bytes cause problems. Why does that happen? EDIT: ah, I left out a 6 in FileOpen... lol Ok so it is now in binary mode, but I still want to be able to see the word "hello" =/ Edited May 11, 2011 by Tsukihime Link to comment Share on other sites More sharing options...
kaotkbliss Posted May 11, 2011 Share Posted May 11, 2011 You probably have to convert back with BinaryToString? 010101000110100001101001011100110010000001101001011100110010000 001101101011110010010000001110011011010010110011100100001 My Android cat and mouse gamehttps://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek We're gonna need another Timmy! Link to comment Share on other sites More sharing options...
smartee Posted May 11, 2011 Share Posted May 11, 2011 hi Tsukihime, Solution (Writing the ANSI representation of the file to the console):#include <String.au3> $file = @ScriptDir & "\NULL(2)hello.txt" ; Create casefile If Not FileExists($file) Then $hnd = FileOpen($file, 17) ; Open for writing binary FileWrite($hnd, StringToBinary(_HexToString("000068656C6C6F"))) FileClose($hnd) EndIf ; Reading casefile $hnd = FileOpen($file, 16) ; Open for reading binary $bin = FileRead($hnd, FileGetSize($file)) For $i = 1 To BinaryLen($bin) ConsoleWrite(BinaryToString(BinaryMid($bin, $i, 1))) Next FileClose($hnd) Explanation: Strings, in AutoIt, are NULL-terminated character arrays, hence, whenever the NULL character is encountered the string is assumed to have ended. See these examples:#include <String.au3> ; NULLNULLhello but nothing is written ConsoleWrite(_HexToString("000068656C6C6F")) ; helloNULLo but "hello" is written ConsoleWrite(_HexToString("68656C6C6F006F")) ; Needless to say, you never see "important message" ConsoleWrite(_HexToString("00") & "important message") Hope this clears some things up -smartee Link to comment Share on other sites More sharing options...
smartee Posted May 11, 2011 Share Posted May 11, 2011 hi,PS: Some functions in AutoIt do read past the NULL characters when dealing with strings, making this a simple workaround:#include <String.au3> $file = @ScriptDir & "\NULL(2)hello.txt" ; Reading casefile $hnd = FileOpen($file, 16) ; Open for reading binary $bin = FileRead($hnd, FileGetSize($file)) ConsoleWrite(StringReplace(BinaryToString($bin), Chr(0), "")) ;Strip out NULL's and print FileClose($hnd)Regards,-smartee Link to comment Share on other sites More sharing options...
kaotkbliss Posted May 11, 2011 Share Posted May 11, 2011 @smartee I think you are too smart for your own good 010101000110100001101001011100110010000001101001011100110010000 001101101011110010010000001110011011010010110011100100001 My Android cat and mouse gamehttps://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek We're gonna need another Timmy! Link to comment Share on other sites More sharing options...
Tsukihime Posted May 11, 2011 Author Share Posted May 11, 2011 (edited) hi, PS: Some functions in AutoIt do read past the NULL characters when dealing with strings, making this a simple workaround:#include <String.au3> $file = @ScriptDir & "\NULL(2)hello.txt" ; Reading casefile $hnd = FileOpen($file, 16) ; Open for reading binary $bin = FileRead($hnd, FileGetSize($file)) ConsoleWrite(StringReplace(BinaryToString($bin), Chr(0), "")) ;Strip out NULL's and print FileClose($hnd) Regards, -smartee Thanks, didn't think about replacing the null's. Edited May 11, 2011 by Tsukihime Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now