Re: hex2seq and seq2hex
Lewis Townsend wrote:
>I'm writing a couple of routines for converting between a hex value
>and a sequence:
>eg: #RRGGBB --> {#RR,#GG,#BB}
>and {#RR,#GG,#BB} --> #RRGGBB
>If anyone has better ones or knows of better ones please post them.
Here's my versions of your routines, "borrowed" somewhat from
int_to_bytes() and bytes_to_int() in machine.e --
function hex2seq(integer x)
-- #RRGGBB is *always* within Euphoria's integer range
integer r, g, b
-- move "backwards" through the rgb value
b = remainder(x, #100)
x = floor(x / #100)
g = remainder(x, #100)
x = floor(x / #100)
r = remainder(x, #100)
return {r,g,b}
end function
function seq2hex(sequence s)
-- identical to yours with Mike's correction
return s[1] * #10000 +
s[2] * #100 +
s[3]
end function
The hex2seq() function above is faster than yours by about 25%.
Hope this helps,
Gabriel Boehme
|
Not Categorized, Please Help
|
|