Re: ASCII (Propellorhead 101)
Re: ASCII (Propellorhead 101)
- Subject: Re: ASCII (Propellorhead 101)
- From: Richard 23 <email@hidden>
- Date: Fri, 19 Jan 2001 00:19:57 -0800
>
hexify_via_file("AbCdEfGhI")
>
>
>
>
If you choose to use this, I will accept both Visa and Master Card
>
for my royalties...
>
>
;-)
Look out, raving madman on the loose (and I don't mean Arthur). Muhahaha!
** PROPELLOR HEAD TRANSMISSION **
** Beanie hats on standby... **
This is the kind of thing I commonly use Ed Lai's Programmer's Tool for.
It slices, it dices, it even burns your toast. Observe:
set theData to read file "Private:desktop folder:StdLog" from 1 to 255
cast theData using template "HBYT" -- hexadecimal byte
--> {"20", "0D", "20", "20", "4D", "61", "63", "73"
cast theData using template "HWRD" -- hexadecimal words (2 byte)
--> {"200D", "2020", "4D61", "6373"}
cast theData using template "HLNG" -- hexadecimal long words (4 byte)
--> {"200D2020", "4D616373"}
If you have a byte which contains bit flags, put on your safety goggles
and:
cast 129 using template "BBIT" -- applescript integers are four
bytes
result's items -8 thru -1 -- this gets just the low order
byte
--> {true, false, false, false, false, false, false, true}
sometimes you just want to take a long dump, often best first thing in the
morning, and output it as hex:
cast theData using template "HEXD"
--> {"200D20204D616373"...}
a little more fiber in your diet may help too.
There's several flavours of data to choose from, how about type names?
(those four-character codes often used to identify file and creator
types).
cast theData using template "TNAM"
--> {<<class >>, <<class Macs>>, <<class Bug >>, <<class 6.6.>>}
Whoops! I'm sorry, you wanted that in decimal. How thoughtless of me.
cast theData using template "HBYT" -- decimal byte
--> {32, 13, 32, 32, 77, 97, 99, 115}
cast theData using template "HWRD" -- decimal words (2 byte)
--> {8205, 8224, 19809, 25459, ...}
cast theData using template "HLNG" -- decimal long words (4 byte)
--> {5.37731104E+8, 1.298228083E+9, ...}
just keep in mind that AppleScript integers are 4 bytes with one of
those bits used to indicate sign, so any number over 32767 is going
to go negative, just like the republican party (heh, just kidding folks).
All works as expected using the first 31 bits:
cast 32767 to "HEXD"
cast result using template "DWRD"
--> 32767
but here's where we encounter our first "Willie Horton":
cast 32768 to "HEXD"
-- hex: "00008000"
cast result using template "DWRD"
--> -32767
cast 65535 to "HEXD"
-- hex: "0000FFFF"
cast result using template "DWRD"
--> {0, -1}
since the basic ResEdit value types (which Programmer's Tool recognizes)
don't include an unsigned decimal word, we have to remember to add 65536
to these negative results. If somebody knows how to get the tool to do
this automatically I'd like to hear about it!
Keeping this quandry in mind, here's a few more basic conversions.
Going from hex to decimal is pretty straightforward...
cast 255 using template "HWRD"
--> {"0000", "00FF"}
cast 65535 using template "HWRD"
--> {"0000", "FFFF"}
But going the other way is sometimes a little disorienting.
set theData to cast {"0000FFFF"} to "HEXD"
cast theData using template "DBYT"
--> {0, 0, 255, 255}
cast theData using template "DLNG"
--> {65535}
cast theData using template "DWRD"
--> {0, -1}
You can do a lot more pointy-headed stuff when you use custom templates
and process composite data but I already put enough people to sleep.
R23