Sign in to follow this  
Followers 0
Tsukihime

Fileread null values

6 posts in this topic

#1 ·  Posted (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 by Tsukihime

Share this post


Link to post
Share on other sites



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 :unsure:

-smartee

Share this post


Link to post
Share on other sites

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

#6 ·  Posted (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 by Tsukihime

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0