繁体   English   中英

通过删除 Javascript 中的前导零来缩短十六进制数

[英]Shortening the Hex number by removing leading zeros in Javascript

我在字符串中有可用的十六进制数,我想通过删除前导零来缩短它。 我需要在 Javascipt 中完成。

例如:

以下所有数字均为十六进制数字。

0000000000000000000000000A000000   -> 00A00000
000000000000000000000000FA000000   -> 000F0A000000

000000000000000000000000C0A80000   -> C0A80000
00000000000000000000003BC0A80000   -> 003BC0A80000

答案中的位数应该是 4 的乘积。像 4 位答案、8 位答案、12 位答案、16 位答案等等……

我的java function是这样的。

// Remove preciding zero like a hex Eg: 0x0000000000000000000000000A000000 -> 0x0A000000

function hexFormatter(hexToShorted) {
    // make shortHex
    var shortedtHex = hexToShorted.replace(/^(0x)0+(0?.*)$/, "$1$2")
    return shortedtHex;
}

您应该先尝试自己,然后发布您所做的事情,我们可以从那里尝试帮助您。 也就是说,这里有一些可以帮助你的东西:

 let test1 = "0x000000000000000C0045" test1 = test1.replace(/^(0x)0+((\w{4})+)$/, "$1$2") console.log(test1) let test2 = "0x0000000000000000C45" test2 = test2.replace(/^(0x)0+((\w{4})+)$/, "$1$2") console.log(test2)

我建议你学习 regex ,它非常有用

暂无
暂无

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

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