簡體   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