在javascript中反转十六进制0x

时间:2017-02-25 15:39:47

标签: javascript hex

我正在使用代码来制作精灵。我有一个正确的精灵,并已手动创建一个左向精灵。我正在寻找一种方法来反转十六进制0x以使精灵面朝左。精灵的一行是0x01555500,反之则是0x05555400。有谁知道如何使用JavaScript做到这一点?

示例:

数组:

[
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00554000, 0x01555500, 0x02AFB000, 0x0BBFBF00,
    0x0BAFEFC0, 0x0AFFAA00, 0x00FFFC00, 0x029A8000,
    0x0A9A6A00, 0x2A956A80, 0x3E75DBC0, 0x3F555FC0,
    0x3D5557C0, 0x01505400, 0x0A802A00, 0x2A802A80
]

反之亦然:

[
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00000000, 0x00000000, 0x00000000, 0x00000000,
    0x00155000, 0x05555400, 0x00EFA800, 0x0FEFEE00,
    0x3FBFAE00, 0x0AAFFA00, 0x03FFF000, 0x002A6800,
    0x0A9A6A00, 0x2A956A80, 0x3E75DBC0, 0x3F555FC0,
    0x3D5557C0, 0x01505400, 0x0A802A00, 0x2A802A80
]

1 个答案:

答案 0 :(得分:2)

你可以用这个:

function reverse(line) {
    line2 = 0;
    for (var i = 0; i < 31; i++) {
        line2 = (line2 << 1) | (line & 1)
        line >>= 1;
    }
    return line2;
}

&#13;
&#13;
function reverse(line) {
    line2 = 0;
    for (var i = 0; i < 31; i++) {
        line2 = (line2 << 1) | (line & 1)
        line >>= 1;
    }
    return line2;
}
// demo:
var line = 0x01555500;
var line2 = reverse(line);
console.log(('0000000' + line.toString(16)).slice(-8));
console.log(('0000000' + line2.toString(16)).slice(-8));
&#13;
&#13;
&#13;