Converting Hexadecimal in Binary

Dar Scott dsc at swcp.com
Sat Jan 10 02:17:00 EST 2004


On Friday, January 9, 2004, at 06:01 PM, Alejandro Tejada wrote:

>> unhex(h) is the string equivalent of a hex sequence
>> function unhex h
>>   return binaryEncode("H*",h)
>> end unhex
>
> This function could help, but I was thinking about
> some way to convert this long hexadecimal string
> in an array or convert the "repeat until empty"
> to "repeat for each".

Maybe this can be simplified:

>   put numtochar(0) after qwe
>   repeat until asd is empty
>     put numtochar(baseconvert(char 1 to 2 of
> asd,16,10)) after qwe
>     put numtochar(baseconvert(char 3 to 4 of
> asd,16,10)) after qwe
>     put numtochar(baseconvert(char 5 to 6 of
> asd,16,10)) after qwe
>     delete char 1 to 6 of asd
>     put numtochar(0) after qwe
>   end repeat
>   delete last char of qwe

Maybe something like this would be faster.

   repeat with i = 1 to length(asd) step 6
     put null & unhex(char i to i+5 of asd) after qwe
   end repeat

To speed this up, you might expand the function call in the expression.

The problem is inserting the extra null every three bytes, otherwise 
this could be done in one step.

It is easy to get data from an image in three bytes per pixel, the 
export as "paint" provides data in P6 format, and that can be trimmed 
to get three bytes per pixel.  The problem is setting the image.  I can 
set it to a PNG value, but not to P6 (binary PPM).

Dar Scott







More information about the use-livecode mailing list