繁体   English   中英

在 vanilla javascript 中将十进制转换为有符号 2 的恭维,反之亦然

[英]convert Decimal to signed 2's compliment and vice versa in vanilla javascript

我对 2 对其工作原理的赞美感到困惑。 我正在制作一个与https://www.rapidtables.com/convert/number/hex-to-decimal.html相同的数字转换器我已经添加了除了这个 2 的恭维之外的所有转换。 这种转换也是十六进制到十进制,十进制到二进制转换,十进制到十六进制转换。 我不太了解这些转换,因此非常需要解释的答案。

下面是我在另一个 js 文件中使用的函数,但是这段代码缺少 2 的恭维。 我不知道如何计算它。 我想要像https://www.rapidtables.com/convert/number/hex-to-decimal.html一样的功能

// =============== Number Converter ===============
// program to convert from any type(decimal,binary or hexadecimal) to any type(binary, decimal or hexadecimal)

export function convertToDecimal(binary) {
    //  use it like this
    // it will return decimal value of binary number
    // convertToDecimal('101') ==> 5
    
    return parseInt(binary, 2);
}


export function convertToBinary(decimal) {
    /* use it like this
    it will return binary value of decimal number
    convertToBinary(5) ==> '101'
    */
    return decimal.toString(2);
}



export function binary2Decimal(binary) {
    /* use it like this
    it will return decimal value of binary number
    binary2Decimal('101') ==> 5
    */
    return parseInt(binary, 2);
}

export function binary2Octal(binary) {
    /* use it like this
    it will return octal value of binary number
    binary2Octal('101') ==> '5'
    */
    return parseInt(binary, 2).toString(8);
}

export function binary2hex(binary) {
    /* use it like this
    it will return hexa value of binary number
    binary2hex('101') ==> '5'
    */
    return parseInt(binary, 2).toString(16);
}


export function fromBinary2Hex(binary) {
    let resp = new Object();
    resp.hex = binary2hex(binary);
    resp.decimal = binary2Decimal(binary);
    return resp;
}

export function hex2Binary(hex) {
    return parseInt(hex, 16).toString(2);
}

export function hex2Decimal(hex) {
    return parseInt(hex, 16);
}

export function fromHex(hex) {
    let resp = new Object();
    resp.binary = hex2Binary(hex);
    resp.decimal = hex2Decimal(hex);
    return resp;
}

export function decimal2Binary(decimal) {
    return parseInt(decimal).toString(2);
}

export function decimal2hex(decimal) {
    return parseInt(decimal).toString(16);
}

export function fromDecimal(decimal) {
    let resp = new Object();
    resp.binary = decimal2Binary(decimal);
    resp.hex = decimal2hex(decimal);
    return resp;
}

看看这个可能会对你有所帮助。

 (function(){ var ConvertBase = function (num) { return { from: function (baseFrom) { return { to: function (baseTo) { return parseInt(num, baseFrom).toString(baseTo); } }; } }; }; // binary to decimal ConvertBase.bin2dec = function (num) { return ConvertBase(num).from(2).to(10); }; // binary to hexadecimal ConvertBase.bin2hex = function (num) { return ConvertBase(num).from(2).to(16); }; // decimal to binary ConvertBase.dec2bin = function (num) { return ConvertBase(num).from(10).to(2); }; // decimal to hexadecimal ConvertBase.dec2hex = function (num) { return ConvertBase(num).from(10).to(16); }; // hexadecimal to binary ConvertBase.hex2bin = function (num) { return ConvertBase(num).from(16).to(2); }; // hexadecimal to decimal ConvertBase.hex2dec = function (num) { return ConvertBase(num).from(16).to(10); }; this.ConvertBase = ConvertBase; })(this); console.log(ConvertBase.bin2dec('101')); console.log(ConvertBase.dec2bin('5')); console.log(ConvertBase.dec2hex('5')); console.log(ConvertBase.hex2bin('5'));

暂无
暂无

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

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