การแปลงไบนารีของ JS แบ่งออกเป็นการแปลงร่วมกันระหว่างไบนารี, แปด - แปดทศนิยมและเลขฐานสิบหก เราสามารถใช้ object.toString () โดยตรงเพื่อนำไปใช้:
เรียกใช้รหัสต่อไปนี้
// แปลงทศนิยมเป็น hexadecimal (10) .tostring (16) // => "a" // แปลงทศนิยมเป็น hexadecimal (012) .ToString (16) // => "A" // เปลี่ยน hexadecimal to toimal (0x16). ภาษาอังกฤษ (0x16) .ToString (8) // => "26" // แปลงทศนิยมเป็นไบนารี // => (1111) .ToString (2) // => "10001010111" // แปลงภาษาอังกฤษเป็นไบนารี // => (01111) binary // => (0x16) .ToString (2) // => "10110"
หากคุณต้องการประมวลผลไบนารีถึงทศนิยม, หกสิบหกถึงทศนิยม, octal ถึงทศนิยมคุณต้องใช้วิธี paresint:
เรียกใช้รหัสต่อไปนี้
// 2 หลักถึงทศนิยม; ParseInt (10,2) // => 2 // 2 หลักถึงทศนิยม; parseint (100,2) // => 4 // hexadecimal ถึงทศนิยม parseint (12, 16) // => 18 // 8-digit ถึงทศนิยม parseint (12, 8); // => 10
การแปลงลำดับความสำคัญ
หากคุณต้องการใช้การแปลงระหว่างตัวเลขคุณสามารถใช้วิธี ParseInt เพื่อแปลงเป็นทศนิยมก่อนจากนั้นใช้ toString (พารามิเตอร์) เพื่อแปลงเป็นตัวเลขที่แตกต่างกัน
เครื่องมือในการตระหนักถึงการแปลงแบบไบนารีโดยใช้วิธีการ ToString และ ParseInt:
เรียกใช้รหัสต่อไปนี้
<! doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> xmlns = "http://www.w3.org/1999/xhtml"> <head> <meta http-equiv = "content-ype" content = "text/html; charset = utf-8"/> <title> num = document.getElementById ("in"). value; var type = document.getElementById ("title"); var tynum, to; สำหรับ (var i = 0; i <type.length; i ++) {ถ้า (ประเภท [i]. selected) tynum = parseint (ประเภท [i] .Value);} สวิตช์ (tynum) {case (1): ถึง = parseint (num) .toString (2); REAK; CASE (3): ถึง = PARSEINT (NUM) .TOSTRING (16); BREAK; CASE (4): ถึง = PARSEINT (NUM, 2); BREAK; CASE (5): ถึง = PARSEINT (NUM, 8); BREAK; CASE (6): ถึง = PARSEINT (NUM, 16); um, 2) .tostring (8); break; case (8): ถึง = parseint (num, 8) .tostring (2); break; case (9): ถึง = parseint (num, 2) .tostring (16); break; case (10): ถึง = parseint (num, 16) : to = parseint (num, 8) .toString (16); break; case (12): to = parseint (num, 16) .toString (8); break;} ถ้า (isnan (ถึง)) ถึง = "อินพุตตัวละคร" ชื่อ "ชื่อ" ชื่อ " ไบนารี </petion> <ตัวเลือกค่า = "2"> ทศนิยมถึงแปดค่า </optup> <ตัวเลือกค่า = "3"> ทศนิยมถึง hex </ตัวเลือก> <ตัวเลือกค่า = "4"> ไบนารีถึงทศนิยม </opotion> <ตัวเลือกค่า = "5"> ตัวเลือก value = "8"> octal to binary </petion> <ตัวเลือกค่า = "9"> ไบนารีถึง hex </opovie> <ตัวเลือกค่า = "10"> hex ถึงไบนารี </opoption> <ตัวเลือกค่า = "10"> hex ถึงไบนารี </option> <ตัวเลือกค่า = "7"> binary to octal </ตัวเลือก hex </petion> <ตัวเลือกค่า = "10"> hex ถึงไบนารี </optup> <ตัวเลือกค่า = "11"> octal ถึง hexadecimal </optup> <ตัวเลือกค่า = "12"> hex ถึง octal </optup> </select> <br/> <อินพุต type = "text" id = "ใน"/> <bbr> onclick = "test ()"/> <font color = "#ff0000" style = "ตัวอักษรขนาด: 12px;">*หมายเหตุ: เมื่อมีอักขระที่ผิดกฎหมายเราจะตัดอักขระที่ถูกต้องสำหรับการแปลง </font> </body> </html>การเข้ารหัสและถอดรหัสอย่างง่าย
แปลงสตริงเป็น Unicode จากนั้นแปลง Unicode เป็นไบนารีอื่นเพื่อใช้การเข้ารหัสรหัส:
เรียกใช้รหัสต่อไปนี้
<! doctype html> <html> <head> <meta charset = "utf-8"/> <title> </head> <body> <body> <body> <body> <body> <body> <body> <body> <body> <body> <body> <body> <body> array (); var i; สำหรับ (i = 0; i <code.length; i ++) monyer+= code.charcodeat (i) .ToString (h)+"_"; // มันคือการแปลงสตริงเป็นรหัส ASCLL แล้วแปลงเป็นจำนวนไบนารีที่คุณต้องการกลับ; code.split ("_"); สำหรับ (i = 0; i <code.length; i ++) {s+= string.fromcharcode (parseint (รหัส [i], h));}; return s}; en ("1wahaha", 8) // = >> "61_52307_52310_52310_" DE ("61_52307_52310_52310_52310 _", 8) // => "1WAHAHA </SCRIPT>อักขระความกว้างเป็นศูนย์
การใช้ความกว้างศูนย์ของอักขระความกว้างเป็นศูนย์เราจะแปลงสตริงทั้งหมดเป็นไบนารีแล้วใช้อักขระความกว้างเป็นศูนย์เพื่อแสดง จากนั้นความยาวสตริงที่สร้างขึ้นจะเป็น 0 ซึ่งสามารถกู้คืนได้โดยการถอดรหัส
เรียกใช้รหัสต่อไปนี้
<! doctype html> <html> <head> <meta charset = "utf-8"/> <title> </title> </head> <body> <body> <body> <body> <body> <body> str.replace (/[^/x00-/xff]/g, ฟังก์ชั่น (a) {// ตัวอักษร transcoding นอกเหนือจากการเข้ารหัสละติน -1 กลับไปหลบหนี (a). retplace ('%', '//');}); str = str.replace (/[/s/s]/g, ฟังก์ชั่น (A) A.Length <8? "/u200c": "01", "/u200d": "10", "/ufeff": "11"}; return string.fromCharcode (parseint (A.Replace (/./ g, ฟังก์ชั่น (a) {return rep [a]}), 2)) ฮ่าฮ่า "); console.log (str.length); console.log (de (str)); </script> </body> </html>ข้างต้นเป็นคำอธิบายที่สมบูรณ์ของการแปลงไบนารีและฟังก์ชั่นใน JS แนะนำให้คุณรู้จักโดยบรรณาธิการ ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณต้องการทราบข้อมูลเพิ่มเติมโปรดใส่ใจกับเว็บไซต์ Wulin.com!