JavaScript - 将24位十六进制数转换为十进制数,加1然后转换回去? [英] JavaScript - Convert 24 digit hexadecimal number to decimal, add 1 and then convert back?

查看:168
本文介绍了JavaScript - 将24位十六进制数转换为十进制数,加1然后转换回去?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于MongoDB中的ObjectId,我使用24位十六进制数字。因为我需要跟踪第二个集合,所以我需要给这个十六进制数字加1。



在我的例子中,这是我的值

  var value =55a98f19b27585d81922ba0b

我在寻找的是

$ $ $ $ $ $ $ $ $ $ $ $ $ $ var $ new $'$ $ $
$



我试图为这个

 创建一个函数,函数hexPlusOne(十六进制){
var num =((0x+ hex)/ 1)+ 1;
返回num.toString(16);
}

这适用于较小的十六进制数字

  hexPlusOne(eeefab)
=> eeefac

但对于我的散列而言,它很失败

  hexPlusOne(value)
=> 55a98f19b275840000000000

有没有更好的方法解决这个问题?
<这个版本只要输入字符串就会返回一个字符串,所以在输入类似ffffffff的情况下忽略这个溢出。



  function hexIncrement(str){var hex = str.match(/ [0-9a-f] / gi); var digit = hex.length; var carry = 1; while(digit& carry){var dec = parseInt(hex [digit],16)+ carry; carry = Math.floor(dec / 16); dec%= 16;十六进制[数字] = dec.toString(16); } return(hex.join());} document.write(hexIncrement(55a98f19b27585d81922ba0b)+< BR>); document.write(hexIncrement(ffffffffffffffffffffffff));  ffffffff结转为100000000。



function hexIncrement(str){var hex = str.match(/ [0-9a -f] / GI); var digit = hex.length; var carry = 1; while(digit& carry){var dec = parseInt(hex [digit],16)+ carry; carry = Math.floor(dec / 16); dec%= 16;十六进制[数字] = dec.toString(16); } if(carry)hex.unshift(1); document.write(hexIncrement(55a98f19b27585d81922ba0b)+< BR>); document.write(hexIncrement(ffffffffffffffffffffffff)); return(hex.join());

我很好奇,看看user2864740对使用12位大块的建议是否会提供任何优势。令我惊讶的是,尽管代码看起来更复杂一些,但实际上其速度是其两倍。但是第一个版本每秒也会运行500,000次,所以它不会像现实世界中那样注意到。



function hexIncrement( str){var result =; var carry = 1; while(str.length& carry){var hex = str.slice(-12); if(/^f*$/i.test(hex)){result = hex.replace(/ f / gi,0)+ result; carry = 1; } else {result =(00000000000+(parseInt(hex,16)+ carry).toString(16))。slice(-hex.length)+ result; carry = 0; } str = str.slice(0,-12); document.write(hexIncrement(55a98f19b27585d81922ba0b)+< BR>); document.write(hexIncrement())返回(str.toLowerCase()+(carry?1:)+ result) 000000000000ffffffffffff)+BR); document.write(hexIncrement(0123456789abcdef000000000000ffffffffffff));


For an ObjectId in MongoDB, I work with a 24 digit hexadecimal number. Because I need to keep track of a second collection, I need to add 1 to this hexadecimal number.

In my case, here's my value

var value = "55a98f19b27585d81922ba0b"

What I'm looking for is

var newValue = "55a98f19b25785d81922ba0c"

I tried to create a function for this

function hexPlusOne(hex) {
    var num = (("0x" + hex) / 1) + 1;
    return num.toString(16);
}

This works with smaller hex numbers

hexPlusOne("eeefab")
=> "eeefac"

but it fails miserably for my hash

hexPlusOne(value)
=> "55a98f19b275840000000000"

Is there a better way to solve this?

解决方案

This version will return a string as long as the input string, so the overflow is ignored in case the input is something like "ffffffff".

function hexIncrement(str) {
    var hex = str.match(/[0-9a-f]/gi);
    var digit = hex.length;
    var carry = 1;

    while (digit-- && carry) {
        var dec = parseInt(hex[digit], 16) + carry;
        carry = Math.floor(dec / 16);
        dec %= 16;
        hex[digit] = dec.toString(16);
    }
    return(hex.join(""));
}

document.write(hexIncrement("55a98f19b27585d81922ba0b") + "<BR>");
document.write(hexIncrement("ffffffffffffffffffffffff"));

This version may return a string which is 1 character longer than the input string, because input like "ffffffff" carries over to become "100000000".

function hexIncrement(str) {
    var hex = str.match(/[0-9a-f]/gi);
    var digit = hex.length;
    var carry = 1;

    while (digit-- && carry) {
        var dec = parseInt(hex[digit], 16) + carry;
        carry = Math.floor(dec / 16);
        dec %= 16;
        hex[digit] = dec.toString(16);
    }
    if (carry) hex.unshift("1");
    return(hex.join(""));
}

document.write(hexIncrement("55a98f19b27585d81922ba0b") + "<BR>");
document.write(hexIncrement("ffffffffffffffffffffffff"));

I was curious te see whether user2864740's suggestion of working with 12-digit chunks would offer any advantage. To my surprise, even though the code looks more complicated, it's actually around twice as fast. But the first version runs 500,000 times per second too, so it's not like you're going to notice in the real world.

function hexIncrement(str) {
    var result = "";
    var carry = 1;
    while (str.length && carry) {
        var hex = str.slice(-12);
        if (/^f*$/i.test(hex)) {
            result = hex.replace(/f/gi, "0") + result;
            carry = 1;
        } else {
            result = ("00000000000" + (parseInt(hex, 16) + carry).toString(16)).slice(-hex.length) + result;
            carry = 0;
        }
        str = str.slice(0,-12);
    }
    return(str.toLowerCase() + (carry ? "1" : "") + result);
}

document.write(hexIncrement("55a98f19b27585d81922ba0b") + "<BR>");
document.write(hexIncrement("000000000000ffffffffffff") + "<BR>");
document.write(hexIncrement("0123456789abcdef000000000000ffffffffffff"));

这篇关于JavaScript - 将24位十六进制数转换为十进制数,加1然后转换回去?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆