Reputation: 105
In my Lua script, I have a variable that stores an integer found from some function that represents a hex string. I want to flip it so that, for example, if it has a value of 0x006E57E8
, it will become 0x8E75E600
. How do I do it in Lua?
Upvotes: 1
Views: 387
Reputation: 11191
As an alternative to Skriptunoff's very concise & readable function, here's a function doing it using bit ops:
function reverse_nibbles(int)
res = 0
for i = 0, 7 do
res = res | (((int >> (4*i)) & 0xF) << (28 - 4*i))
end
return res
end
This won't create a garbage string (and would hopefully be faster). You might want to hand-unroll the loop.
Upvotes: 2