ฉันต้องการแปลงสตริงไบนารีเป็นตัวเลขเช่น
var binary = "1101000" // code for 104
var digit = binary.toString(10); // Convert String or Digit (But it does not work !)
console.log(digit);
มันเป็นไปได้ยังไงกัน? ขอบคุณ
ฉันต้องการแปลงสตริงไบนารีเป็นตัวเลขเช่น
var binary = "1101000" // code for 104
var digit = binary.toString(10); // Convert String or Digit (But it does not work !)
console.log(digit);
มันเป็นไปได้ยังไงกัน? ขอบคุณ
คำตอบ:
parseIntฟังก์ชันแปลงสายไปยังหมายเลขและมันต้องใช้อาร์กิวเมนต์ที่สองระบุฐานที่แสดงสตริงคือ:
var digit = parseInt(binary, 2);
ดูการใช้งานจริง
parseInt. ฉันคิดว่ามันจะแปลงสตริงจากฐาน 10 -> อะไรก็ได้ (คิดว่าparseInt('5612', 2)จะคืนรูปแบบไบนารี;)
ES6 รองรับลิเทอร์ตัวเลขไบนารีสำหรับจำนวนเต็มดังนั้นหากสตริงไบนารีไม่เปลี่ยนรูปดังเช่นในโค้ดตัวอย่างในคำถามก็สามารถพิมพ์ได้ตามที่มีคำนำหน้า0bหรือ0B:
var binary = 0b1101000; // code for 104
console.log(binary); // prints 104
parseInt() ด้วย radix เป็นทางออกที่ดีที่สุด (ตามที่หลายคนบอก):
แต่ถ้าคุณต้องการใช้งานโดยไม่ต้อง parseInt นี่คือการใช้งาน:
function bin2dec(num){
return num.split('').reverse().reduce(function(x, y, i){
return (y === '1') ? x + Math.pow(2, i) : x;
}, 0);
}
function binaryToDecimal(string) {
let decimal = +0;
let bits = +1;
for(let i = 0; i < string.length; i++) {
let currNum = +(string[string.length - i - 1]);
if(currNum === 1) {
decimal += bits;
}
bits *= 2;
}
console.log(decimal);
}
var num = 10;
alert("Binary " + num.toString(2)); //1010
alert("Octal " + num.toString(8)); //12
alert("Hex " + num.toString(16)); //a
alert("Binary to Decimal "+ parseInt("1010", 2)); //10
alert("Octal to Decimal " + parseInt("12", 8)); //10
alert("Hex to Decimal " + parseInt("a", 16)); //10
ฉันรวบรวมสิ่งที่คนอื่นแนะนำและสร้างฟังก์ชันต่อไปนี้ซึ่งมี 3 อาร์กิวเมนต์จำนวนและฐานที่มาจากจำนวนนั้นและฐานที่ตัวเลขนั้นจะอยู่:
changeBase(1101000, 2, 10) => 104
เรียกใช้ Code Snippet เพื่อลองด้วยตัวคุณเอง:
function changeBase(number, fromBase, toBase) {
if (fromBase == 10)
return (parseInt(number)).toString(toBase)
else if (toBase == 10)
return parseInt(number, fromBase);
else{
var numberInDecimal = parseInt(number, fromBase);
return (parseInt(numberInDecimal)).toString(toBase);
}
}
$("#btnConvert").click(function(){
var number = $("#txtNumber").val(),
fromBase = $("#txtFromBase").val(),
toBase = $("#txtToBase").val();
$("#lblResult").text(changeBase(number, fromBase, toBase));
});
#lblResult{
padding: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="txtNumber" type="text" placeholder="Number" />
<input id="txtFromBase" type="text" placeholder="From Base" />
<input id="txtToBase" type="text" placeholder="To Base" />
<input id="btnConvert" type="button" value="Convert" />
<span id="lblResult"></span>
<p>Hint: <br />
Try 110, 2, 10 and it will return 6; (110)<sub>2</sub> = 6<br />
or 2d, 16, 10 => 45 meaning: (2d)<sub>16</sub> = 45<br />
or 45, 10, 16 => 2d meaning: 45 = (2d)<sub>16</sub><br />
or 2d, 2, 16 => 2d meaning: (101101)<sub>2</sub> = (2d)<sub>16</sub><br />
</p>
FYI: หากคุณต้องการส่ง 2d เป็นเลขฐานสิบหกคุณต้องส่งเป็นสตริงจึงจะเป็นดังนี้:
changeBase('2d', 16, 10)
การใช้งานอื่นสำหรับการฝึก JS ที่ใช้งานได้อาจเป็นได้
var bin2int = s => Array.prototype.reduce.call(s, (p,c) => p*2 + +c)
console.log(bin2int("101010"));
+cบังคับให้Stringพิมพ์cเป็นNumberค่าประเภทสำหรับการเพิ่มที่เหมาะสม
อัลกอริทึมการแปลงไบนารีแบบเดิมที่ปรับเปลี่ยนเล็กน้อยโดยใช้ไวยากรณ์ ES6 และคุณลักษณะอัตโนมัติเพิ่มเติม:
แปลงสตริงลำดับไบนารีเป็น Array (สมมติว่ายังไม่ได้ส่งผ่านเป็นอาร์เรย์)
ลำดับย้อนกลับเพื่อบังคับให้ 0 ดัชนีเริ่มต้นที่เลขฐานสองทางขวาสุดเนื่องจากไบนารีคำนวณจากขวา - ซ้าย
ฟังก์ชัน 'ลด' Array ข้ามอาร์เรย์โดยทำการรวม (2 ^ index) ต่อเลขฐานสอง [เฉพาะเมื่อเลขฐานสอง === 1] (0 หลักจะให้ผลเป็น 0 เสมอ)
หมายเหตุ: สูตรการแปลงไบนารี:
{โดยที่ d = เลขฐานสอง, i = ดัชนีอาร์เรย์, n = อาร์เรย์ยาว -1 (เริ่มจากขวา)}
n
∑ (d * 2 ^ i)
ผม = 0
let decimal = Array.from(binaryString).reverse().reduce((total, val, index)=>val==="1"?total + 2**index:total, 0);
console.log(`Converted BINARY sequence (${binaryString}) to DECIMAL (${decimal}).`);
parseInt(101, 2)5