Binary files have got me beat!

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Binary files have got me beat!

Post by n.allan » Mon Mar 12, 2007 10:07 pm

I have been using runrev studio for about a month now and have found it very intuative to use while parsing text files into other formats, but when it comes to binary (hex) I am totally confused.

I want to read a binary file containg encrypted latitude and longditude data at offset $200 for 1 byte, evaluate the hex value of that byte, then depending on the hex value of that char, I would read then next 4 bytes into a variable say varLatitude then then the next 4 bytes into varEncryptedLongditude. To decrypt this data I need to XOR the two variables, the 4 byte answer would be the decrypted Longditude.

Now another problem is that these specific 4 byte strings are in 2's compliment notation (negative numbers being south and west, positive bieng north and east) they also appear to be written to the file back to front, (I believe this is something to do with Big Endian or Little endian)

The data was created on win32 and my runrev project will ony ever run on win32 so at least there are no cross platform issues to worry about here?

Maybe I have bitten off more than i can chew here for a begginer? But I really want to push my abilities in this area and i find the best way is to dive in!

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Post by BvG » Fri Mar 16, 2007 4:55 am

I can't tell you how exactly to do this, but I can give you some info that could get you started:

First you need to get to your binary data. If it's not a huge multy megabytes file, you can put the whole thing into a var (but not a field, as fields mangle binary).

put url "binfile:path/to/your/file.binary" into yourBinaryData
local ignoredJunk, theResultingVar
BinaryDecode(x300a4,yourBinaryData,ignoredJunk,theResultingVar)

With binarydecode, you can translate almost every binary data to something more usefull. The above example ignores the first 300 bytes, reads 4 bytes, converts them to characters and puts the characters into theResultingVar. BinaryDecode is a bit special, in that the actual data is put into a variable you specify, also note that the variable must already exist!
you can try other conversions, if this doesn't give you expected results, so read the documentation entry carefully to understand the differences. Especially tak care of the note about "a" && anyNumber always giving you one var, whereas most others give you anyNumber of different vars.
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Post by n.allan » Fri Mar 16, 2007 10:44 am

Thanks for this BvG, I only realised i'd put this into the wrong section of the forum.

The problems I had may have resulted from the fact that the varVariable must exist before using it in binaryencode,binarydecode.

Post Reply