Data to hexadecimal conversion

I need a scriptlet to turn data values like:

<d8 27 00 00>

Into hexadecimal values like:


Basically, you take out all the spaces and the “<” and “>” from the data value, and then you read the numbers backwards, and add “0x” to the beginning.


set x to "<d8 27 00 00>"
set y to characters 2 thru -2 of x as text
set z to ""
repeat with i from 4 to 1 by -1
	set z to z & word i of y
end repeat
set z to "0x" & z
display dialog z