简体   繁体   English

如何翻转代表 Lua 中的 8 位十六进制字符串的 integer?

[英]How to flip an integer that represents a 8-digit hex string in Lua?

In my Lua script, I have a variable that stores an integer found from some function that represents a hex string.在我的 Lua 脚本中,我有一个变量,它存储从一些 function 中找到的 integer,它表示一个十六进制字符串。 I want to flip it so that, for example, if it has a value of 0x006E57E8 , it will become 0x8E75E600 .我想翻转它,例如,如果它的值为0x006E57E8 ,它将变为0x8E75E600 How do I do it in Lua?我如何在 Lua 中做到这一点?

As an alternative to Skriptunoff's very concise & readable function, here's a function doing it using bit ops:作为 Skriptunoff 非常简洁易读的 function 的替代方案,这里有一个 function 使用位操作:

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.您可能想要手动展开循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM