将十进制转换为带符号的 2 的补码,反之亦然 javascript
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 的赞美之外的所有转换。这种转换也是在所有十六进制到十进制,十进制到二进制转换,十进制到十六进制转换。我不是很了解这些转换,所以解释的答案非常合适。
below is the functions which i use in another js file, but this code missing 2's compliment. i have no idea on how do i calculate it. i want the same functionality like this 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'));
我对 2 对它的工作原理的赞美深信不疑。我正在制作一个与 https://www.rapidtables.com/convert/number/hex-to-decimal.html 相同的数字转换器 我已经添加了除此 2 的赞美之外的所有转换。这种转换也是在所有十六进制到十进制,十进制到二进制转换,十进制到十六进制转换。我不是很了解这些转换,所以解释的答案非常合适。
below is the functions which i use in another js file, but this code missing 2's compliment. i have no idea on how do i calculate it. i want the same functionality like this 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'));