ThomQW Posted July 1, 2010 Share Posted July 1, 2010 Hi, Can some help please? I need to read a two-byte int from a file at a specific position and it's not working. Here is my code: Func getInfo() Dim $file Dim $pName Dim $pAcct = 0 ; open in text mode $file = FileOpen("TEST.FIL", 0) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open test file.") Exit EndIf ; get name FileSetPos($file,768,0) $pName=FileRead($file,32) MsgBox(1,"Test1",$pName) FileClose($file) ; open in binary mode $file = FileOpen("TEST.FIL", 16) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open test file.") Exit EndIf ; get acct# $pAcct = 0 FileSetPos($file,1803,0) $pAcct=FileRead($file,2) MsgBox(1,"Test2",BinaryToString($pAcct,2)) FileClose($file) EndFunc Hope someone can help. Thanks, ThomQW Link to comment Share on other sites More sharing options...
jchd Posted July 1, 2010 Share Posted July 1, 2010 BinaryToString doesn't do what you seem to think it does:Local $x = Binary('0x3456') ConsoleWrite(BinaryToString($x) & @LF) ConsoleWrite(Hex($x) & @LF) ConsoleWrite(String($x) & @LF) ConsoleWrite(Number($x) & @LF)Then it also depends if the word you want to read is stored as little- or big-endian, and you might want to read it the required way.You can use the functions below to read 16- or 32-bit integers with programmable endianness:Local $h = FileOpen($filename, 16) <add error checking> Local $uint16, $uint32 FileSetPos(<as needed>) + error checking! $uint16 = FileRead($h, 2) If @error Then <error> $uint32 = _Read32($h, $endian) If @error Then <error> FileClose($h) Func _Read16($hdl, $littleEndian) Local $u16 = FileRead($hdl, 2) If @error Then Return(SetError(@error, 0, 0)) If $littleEndian Then Return(Number($u16)) Else Return(Number(BitOR(BitShift($u16, 8), BitShift(BitAND($u16, 0xFF), -8)))) EndIf EndFunc Func _Read32($hdl, $littleEndian) If $littleEndian Then Local $u32 = FileRead($hdl, 4) Return(SetError(@error, 0, Number($u32))) Else Local $u16_1 = _Read16($hdl, $littleEndian) If @error Then Return(SetError(@error, 0, 0)) Local $u16_2 = _Read16($hdl, $littleEndian) If @error Then Return(SetError(@error, 0, 0)) Return(Number(BitOR(BitShift($u16_1, -16), $u16_2))) EndIf EndFunc This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
ThomQW Posted July 1, 2010 Author Share Posted July 1, 2010 Used your code, worked great! Thanks for the fast response. It saved me a lot of time! Thanks again, ThomQW 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