Tsukihime 0 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 Share this post Link to post Share on other sites
kaotkbliss 146 Posted May 11, 2011 You probably have to convert back with BinaryToString? 010101000110100001101001011100110010000001101001011100110010000001101101011110010010000001110011011010010110011100100001My Android cat and mouse gamehttps://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueekWe're gonna need another Timmy! Share this post Link to post Share on other sites
smartee 14 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 Share this post Link to post Share on other sites
smartee 14 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 Share this post Link to post Share on other sites
kaotkbliss 146 Posted May 11, 2011 @smartee I think you are too smart for your own good 010101000110100001101001011100110010000001101001011100110010000001101101011110010010000001110011011010010110011100100001My Android cat and mouse gamehttps://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueekWe're gonna need another Timmy! Share this post Link to post Share on other sites
Tsukihime 0 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 Share this post Link to post Share on other sites