BinaryEncode

Michael Kann mikekann at yahoo.com
Sun Jan 15 22:39:53 EST 2012


Rick,

A little more explanation. The numToChar function takes an integer. In your case if you feed the function the integer 119 then it spits out "w" -- which is what you want. At the end of your script you ended up with "01110111", which is the way we "spell" the binary number 119 -- using ones and zeroes. But the numToChar function only works on integers, not the ones and zeroes. So you need one more conversion step to get back to the integer before you use the numToChar function. 

It looks like if you feed the numToChar nonsense, you get nonsense back without any warning.

Mike


on mouseUp
put numToChar("01110111") into x
put numToChar(119) into y
put x && y into fld 1
end mouseUp

fld 1 will then contain "_ w"









--- On Sun, 1/15/12, Rick Harrison <harrison at all-auctions.com> wrote:

From: Rick Harrison <harrison at all-auctions.com>
Subject: BinaryEncode
To: "How to use LiveCode" <use-livecode at lists.runrev.com>
Date: Sunday, January 15, 2012, 6:44 PM

I was trying some tests with binary numbers.

If I put in "w" and convert it to binary I get
"01110111" which is correct.  If I then write
it out to a file, and read it back in again and
then convert it back I get "01110111" correctly,
but when converted to a character I get a "_"
which is completely wrong.

Thanks,

Rick

Code follows:

set the defaultFolder to specialFolderPath ("desktop") 
   put "w" into varText
   answer "varText = " & varText  
     repeat for each char tChar in varText
           put charToNum(tChar) into theNum
           put baseConvert(theNum,10,2) into tBaseConverted
           put char -8 to -1 of ("00000000" & tBaseConverted ) into tBaseConverted
           put tBaseConverted into tConverted
     end repeat
     answer "tConverted = " & tConverted   
   put binaryencode("a*",tConverted) into VarBinaryCodedResult
   put the defaultFolder & "/" & "TestFile" into TestFileName  
   open file TestFileName for binary write
   write VarBinaryCodedResult to file TestFileName
   close file TestFileName   
   open file TestFileName for binary read
   read from file TestFileName until EOF
   close file TestFileName
    put it into VarTestFileData  
    put binaryDecode(a8,VarTestFileData,VarDecoded) into VarDisplayData
    answer "VarDecoded = " & VarDecoded
    answer "VarDisplayData = " & VarDisplayData
    put the numToChar of VarDecoded into CharToDisplay
    answer "CharToDisplay = " & CharToDisplay
_______________________________________________
use-livecode mailing list
use-livecode at lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
_______________________________________________
use-livecode mailing list
use-livecode at lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode



More information about the use-livecode mailing list