如何在javascript中将整数数组转换为十六进制字符串,反之亦然

发布于 2024-12-08 02:57:53 字数 192 浏览 3 评论 0原文

我在 javascript 中有 32 位整数数组。 如何将其转换为十六进制字符串 并在需要时再次从该十六进制字符串构建相同的 32 位整数数组?

hexString = yourNumber.toString(16); 可用于将数字转换为十六进制 但是当数字数组转换为十六进制字符串(它将是连续的或由某个字符分隔)时,如何从该字符串中获取数字数组?

I have array of 32 bit integers in javascript.
How to convert it into the Hex string
and again build the same 32 bit integer array from that Hex string when required?

hexString = yourNumber.toString(16);
can be used to convert number to hex
but when array of numbers is converted into Hex string (it will be continuous or separated by some character) then how do I get back array of numbers from that string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

朦胧时间 2024-12-15 02:57:53

如果你想不加逗号

[3546,-24,99999,3322] ==> "00000ddaffffffe80001869f00000cfa"

然后您可以为每个数字使用 8 个十六进制数字构建字符串。当然,您必须对短于 8 个十六进制数字的数字进行零填充。并且您必须确保数字使用 twos-compliment 进行编码才能正确处理任何负值。

具体操作方法如下:

var a = [3546,-24,99999,3322];
alert("Original is " + JSON.stringify(a));    // [3546,-24,99999,3322]


// convert to hex string...
//
var b = a.map(function (x) {
    x = x + 0xFFFFFFFF + 1;  // twos complement
    x = x.toString(16); // to hex
    x = ("00000000"+x).substr(-8); // zero-pad to 8-digits
    return x
}).join('');
alert("Hex string " + b);   // 00000ddaffffffe80001869f00000cfa


// convert from hex string back to array of ints
//
c = [];
while( b.length ) {
    var x = b.substr(0,8);
    x = parseInt(x,16);  // hex string to int
    x = (x + 0xFFFFFFFF + 1) & 0xFFFFFFFF;   // twos complement
    c.push(x);
    b = b.substr(8);
}
alert("Converted back: " + JSON.stringify(c));    // [3546,-24,99999,3322]

这里有一个 jsFiddle,它显示了上述示例。

If you want to do it without commas

[3546,-24,99999,3322] ==> "00000ddaffffffe80001869f00000cfa"

then you can build up the string using 8 hex-digits for each number. Of course, you'll have to zero-pad numbers that are shorter than 8 hex-digits. And you'll have to ensure that the numbers are encoded with twos-compliment to properly handle any negative values.

Here's how to do that:

var a = [3546,-24,99999,3322];
alert("Original is " + JSON.stringify(a));    // [3546,-24,99999,3322]


// convert to hex string...
//
var b = a.map(function (x) {
    x = x + 0xFFFFFFFF + 1;  // twos complement
    x = x.toString(16); // to hex
    x = ("00000000"+x).substr(-8); // zero-pad to 8-digits
    return x
}).join('');
alert("Hex string " + b);   // 00000ddaffffffe80001869f00000cfa


// convert from hex string back to array of ints
//
c = [];
while( b.length ) {
    var x = b.substr(0,8);
    x = parseInt(x,16);  // hex string to int
    x = (x + 0xFFFFFFFF + 1) & 0xFFFFFFFF;   // twos complement
    c.push(x);
    b = b.substr(8);
}
alert("Converted back: " + JSON.stringify(c));    // [3546,-24,99999,3322]

here's a jsFiddle that shows the above example.

魔法唧唧 2024-12-15 02:57:53

给你:

var a = [3546,-24,99999,3322];
alert("Original is " + JSON.stringify(a));
var b = (a.map(function (x) {return x.toString(16);})).toString();
alert("Hex string " + b);
var c = b.split(",").map(function (x) {return parseInt(x, 16);});
alert("Converted back: " + JSON.stringify(c));

http://jsfiddle.net/whT2m/

ADDENDUM

OP询问是否使用逗号以外的分隔符。只需要稍作调整即可。这是使用分号的版本:

var a = [3546,-24,99999,3322];
alert("Original is " + JSON.stringify(a));
var b = (a.map(function (x) {return x.toString(16);})).join(";");
alert("Hex string " + b);
var c = b.split(";").map(function (x) {return parseInt(x, 16);});
alert("Converted back: " + JSON.stringify(c));

http://jsfiddle.net/DEbUs/

Here you go:

var a = [3546,-24,99999,3322];
alert("Original is " + JSON.stringify(a));
var b = (a.map(function (x) {return x.toString(16);})).toString();
alert("Hex string " + b);
var c = b.split(",").map(function (x) {return parseInt(x, 16);});
alert("Converted back: " + JSON.stringify(c));

http://jsfiddle.net/whT2m/

ADDENDUM

The OP asked about using a separator other than a comma. Only a small tweak is needed. Here is a version using the semicolon:

var a = [3546,-24,99999,3322];
alert("Original is " + JSON.stringify(a));
var b = (a.map(function (x) {return x.toString(16);})).join(";");
alert("Hex string " + b);
var c = b.split(";").map(function (x) {return parseInt(x, 16);});
alert("Converted back: " + JSON.stringify(c));

http://jsfiddle.net/DEbUs/

小耗子 2024-12-15 02:57:53
var ints = [1,2,30,400];
var hexstr = "";
for(var i=0; i < ints.length; i++) {
    hexstr += ints[i].toString(16) + ";";
}

document.write("Hex: " + hexstr + "<br>");
var intsback = new Array();
var hexarr = hexstr.split(";");
for(var i = 0; i < hexarr.length-1; i++) {
    intsback.push(parseInt(hexarr[i], 16));
}

document.write("Integers back: " + intsback);

http://jsfiddle.net/YVdqY/

var ints = [1,2,30,400];
var hexstr = "";
for(var i=0; i < ints.length; i++) {
    hexstr += ints[i].toString(16) + ";";
}

document.write("Hex: " + hexstr + "<br>");
var intsback = new Array();
var hexarr = hexstr.split(";");
for(var i = 0; i < hexarr.length-1; i++) {
    intsback.push(parseInt(hexarr[i], 16));
}

document.write("Integers back: " + intsback);

http://jsfiddle.net/YVdqY/

心舞飞扬 2024-12-15 02:57:53

这里有两个使用纯 JavaScript 进行转换的函数,它们应该适用于所有浏览器:

var nums = [3456, 349202, 0, 15, -4567];

function convertNumArrayToHexString(list) {
    var result = [];
    for (var i = 0; i < list.length; i++) {
        result.push(list[i].toString(16));
    }
    return(result.join(","));
}

function convertHexStringToNumArray(str) {
    var result = [];
    var list = str.split(/\s*,\s*/);
    for (var i = 0; i < list.length; i++) {
        result.push(parseInt(list[i], 16));
    }
    return(result);
}


var temp = convertNumArrayToHexString(nums);
temp = convertHexStringToNumArray(temp);

并且,一个工作演示:http: //jsfiddle.net/jfriend00/3vmNs/

Here are two functions to do the conversions using plain javascript that should work in all browsers:

var nums = [3456, 349202, 0, 15, -4567];

function convertNumArrayToHexString(list) {
    var result = [];
    for (var i = 0; i < list.length; i++) {
        result.push(list[i].toString(16));
    }
    return(result.join(","));
}

function convertHexStringToNumArray(str) {
    var result = [];
    var list = str.split(/\s*,\s*/);
    for (var i = 0; i < list.length; i++) {
        result.push(parseInt(list[i], 16));
    }
    return(result);
}


var temp = convertNumArrayToHexString(nums);
temp = convertHexStringToNumArray(temp);

And, a working demo: http://jsfiddle.net/jfriend00/3vmNs/

月竹挽风 2024-12-15 02:57:53

十六进制字符串到整数数组(例如,0E0006006200980000000000000000)

dataToNumberArr(data) {
        if(data.length == 2) {
            let val = parseInt(data, 16);
            return [val];
        } else if(data.length > 2) {
            return dataToNumberArr(data.substr(0, 2)).concat(dataToNumberArr(data.substr(2)));
        }
    }

Hex string to integer array(ex, 0E0006006200980000000000000000)

dataToNumberArr(data) {
        if(data.length == 2) {
            let val = parseInt(data, 16);
            return [val];
        } else if(data.length > 2) {
            return dataToNumberArr(data.substr(0, 2)).concat(dataToNumberArr(data.substr(2)));
        }
    }
柒夜笙歌凉 2024-12-15 02:57:53

使用此:

parseInt(hexstr, 16);

来源:http://javascript.about.com/library/blh2d.htm

Use this:

parseInt(hexstr, 16);

Source: http://javascript.about.com/library/blh2d.htm

夜空下最亮的亮点 2024-12-15 02:57:53

您可以调用 parseInt(hexString, 16) 将十六进制字符串转换为整数值。

You can call parseInt(hexString, 16) to convert a hex string to an integer value.

茶底世界 2024-12-15 02:57:53
let ints = [1,2,30,400];
let hex = parseInt(ints.reverse().map(n => n.toString(16)).join(''), 16)

如果你想使用像“400”这样的大数字(> 255),你必须为每个数组成员添加填充

let ints = [1,2,30,400];
let hex = parseInt(ints.reverse().map(n => n.toString(16)).join(''), 16)

If you want to use large numbers (>255) like "400" you have to add padding to every array member

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文