problem with BinaryDecode

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

problem with BinaryDecode

Post by Simon Knight » Sat Mar 05, 2011 9:37 am

I am starting a new project that will require the manipulation of binary data stored in files. I wish to display the contents of the file as a list of hex digits in the same way that a hex editor does. I have loaded my file and am attempting to use BinaryDecode to convert the display of the file to hex but I am unable to get the code to work. Will someone explain what I am doing wrong as I find the dictionary explanation of binarydecode rather confusing.

Here is all my code which is run when a button is pushed:

Code: Select all

On LoadFile
   put "" into fld"debug"
   --Prompt for input file (note the input file is a binary flle with no line endings
   answer file "Select the text file" with type "mux"
   if the result is "Cancel" then exit LoadFile
   
   put "binfile:" & it into tURL      -- merges the user selected filepath with the binary file command prefix ready for next command:
   put URL tURL into tFileData     -- store the contents of file into the variable tFileData
   -- routine can now work with a local variable rather than the file
   put tFiledata into fld"debug"
   
   --try to convert the binary data to hex
   --first create a variable
   put "" into tHex
   
   put binaryDedcode("h*",tFiledata, tHex) into fld"debug"  -- does not work
   
end LoadFile
best wishes
Simon
best wishes
Skids

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

Re: problem with BinaryDecode

Post by BvG » Sat Mar 05, 2011 2:17 pm

The binaryencode and decode commands are one of the most confusing there are in LC.But your code is almost right. The problem is that the function itself returns something unusable, just the amount of converted chunks, which varies depending on what kind of data you want to create. That is why you need to give it a pre-existing variable.

So simply add the following line at the end:

Code: Select all

put tHex into field "debug"
Various teststacks and stuff:
http://bjoernke.com

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

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: problem with BinaryDecode

Post by Simon Knight » Sat Mar 05, 2011 4:50 pm

Hi,
The binaryencode and decode commands are one of the most confusing there are in LC
You can type that again!

I have found a tutorial about reading binary files at http://lessons.runrev.com/spaces/lesson ... nary-File- and by Trevor Devor. My code is now working but I still don't understand quite why :?: , based on the dictionary description of binaryDecode, my code now looks like this :

Code: Select all

On LoadFile 
   put "" into fld"debug"
   --Prompt for input file (note the input file is a binary flle with no line endings
   answer file "Select the text file" with type "mux"
   if the result is "Cancel" then exit LoadFile
   
   put "binfile:" & it into tURL      -- merges the user selected filepath with the binary file command prefix ready for next command:
   put URL tURL into theBinaryData     -- store the contents of file into the variable tBinaryData

   put "" into tHex  --the conversion routine needs a var
   
  -- put binaryDedcode("h*",tFiledata, tHex) into fld"debug"  -- does not work
   ## Assume that binary data is stored in the variable theBinaryData
put 1 into tCount  -- I will only process the first 16 bytes of data (at present)

--parse first 16 bytes and display as a list and also on a line in a field
repeat until tCount=17
   
   put byte tCount of theBinaryData into theByte  --where is byte defined, dictionary search does not find it ?
   
   put theByte after theText  --add the byte (a single character) to a string
  
   put binarydecode("H*", theByte, tHex) into theNumConversions  -- from tutorial, tHex is the value needed, what is theNumConversions for.
   put tHex & " " after theHex  -- store the hex decode for future display on a line
   
  put tHex after fld"debug"  -- display hex value in the field
  put "- " & theByte & "-- " & theNumConversions & CR after fld"debug"  -- display character as well.
  put tCount+1 into tCount  -- next byte
end repeat
-- simulated hex editor type display single line
put theHex & " == " & theText after fld"debug"
end LoadFile
The line "put binarydecode("H*", thebyte, tHex) into theNumConversions is taken from the tutorial. NumConversions is set to 1 , why?
The dictionary describes the first parameter of the binary decode as a formatlist, the code above uses H. This is defined as " H: convert next amount bytes of data to hexadecimal digits, starting at the high end of each byte"

The first character in my test file is "@" and my hex editor and the web decodes it as h40 or d64. I interpret the dictionary definition to require me to enter the number of bytes to be converted which in this case is 1. If I enter H1 tHex is set to 4 rather than 40, if I enter H2 then tHex is set correctly to 40. The dictionary also includes an important note which boils down to an example of using h3 as the formatlist. It states that h3 requires 3 variables to be defined as each byte requires its own variable, this seems to be contrary to what I am seeing as H2 or h2 causes a single byte to be decoded into a two character hex number. The rest of the dictionary entry seems to be designed to confuse e.g.
The amount corresponding to each dataType is an integer or the * character.
then
If you specify an amount with a stringdataType (a or A),
but you just said its an integer......? Any comments as my head hurts ?

Simon
best wishes
Skids

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

Re: problem with BinaryDecode

Post by BvG » Sat Mar 05, 2011 5:36 pm

I think the return value of the function (you said it's 1) is the amount of chunks processed. as you specified "H" that should be 1. The actual value is put into the variable you specify, which in all of your examples is 'tHex'.

When using h or H in binaryDecode, you're specifying the amount of output hex characters. however, one byte is two hex characters (!). so if you use one, you get 4, where you should use 2 and then get 40. On second thought maybe it's not characters but places within the hex number... Yes, very weird and confusing.
Various teststacks and stuff:
http://bjoernke.com

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

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

Re: problem with BinaryDecode

Post by BvG » Sat Mar 05, 2011 5:39 pm

Simon Knight wrote:
The amount corresponding to each dataType is an integer or the * character.
then
If you specify an amount with a stringdataType (a or A),
but you just said its an integer......? Any comments as my head hurts ?
Right forgot this one. The amount is an integer, the string DataTypes can use amounts, which are integers (or a star character "*"). The numeric types also use integers. You're easily confused :P
Various teststacks and stuff:
http://bjoernke.com

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

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: problem with BinaryDecode

Post by Simon Knight » Sat Mar 05, 2011 6:59 pm

Hmmm that clear then...... and here I was thinking that functions returned single values.

Seems the dictionary entry has been written by someone who has english as a second language (a Scot perhaps :mrgreen: ), the whole function seems a little unfinished.

Thanks for your inputs,

Simon
best wishes
Skids

Simon Knight
Posts: 919
Joined: Wed Nov 04, 2009 11:41 am

Re: problem with BinaryDecode

Post by Simon Knight » Sat Mar 05, 2011 8:02 pm

I have spent (wasted?) most of today experimenting with two aspects of BinaryDecode; reading bytes as hex values and as binary values. The documentation is very poor and in error. If the legal parameters entered are not liked the function seems to get easily confused resulting in infinite loops.

My need is to manipulate 16 bit data words stored in a file and if I end up using binarydecode I will only use it on single bytes (characters) to convert the data to stings of 1s and 0s as it seems more robust when it only has to process a single byte, I will then use the string routines to extract the required bits.

Note to self: use with caution!

Simon
best wishes
Skids

Post Reply