繁体   English   中英

将5位数的二进制字符串解码回字符

[英]Decode 5-digit binary string back into character

我创建了一个简单的函数,该函数可以将每个字母字符( az )转换为always 5-digit binary string (like "00000->a" or "11000->y")

SWIFT

 let alphabeth_array=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "", "", "", "", "", ""]
 for i in 0..<32 {
   let binary_code = String(i/16%2) + String(i/8%2) + String(i/4%2) + String(i/2%2) + String(i%2)
   print("\(alphabeth_array[i]) : \(binary_code)")
}



JAVASCRIPT

 var alphabeth_array=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "", "", "", "", "", ""]
 for (var i=0; i<32; i++) {
   var binary_code = (i/16%2).toString() + (i/8%2).toString() + (i/4%2).toString() + (i/2%2).toString() + (i%2).toString()
   console.log(alphabeth_array[i] + " : " + binary_code)
}



但现在我想的二进制字符串解码成从他们的关联人物alphabeth_array你可以找到以上。

SWIFT

let binary = "00000" //starting with the binary string
let character = getChar(binaryCode: binary)

func getChar(binaryCode: String) -> (String) {
   // programm logic
   return result
}



JAVASCRIPT

var binary = "00000" //starting with the binary string
var character = getChar(binary)


function getChar(binary) {
   // programm logic
   return result
}

我完全不知道如何反转解码功能以将二进制字符串转换回连接的字符。 任何帮助将不胜感激,谢谢! (我希望只获取一些小的代码片段,Swift或javascript-哪种语言都没有关系!)

您可以解析基数为2的字符串,添加10的偏移量,然后将值转换为基数为36的值。

 function getCharacter(string) { return (parseInt(string, 2) + 10).toString(36); } function getBinary(string) { return ('000000' + (parseInt(string, 36) - 10).toString(2)).slice(-5); } console.log(getCharacter('00000')); // a console.log(getCharacter('11000')); // y console.log(getBinary('a')); // 00000 console.log(getBinary('y')); // 11000 

您可以使用init?<S>(_ text: S, radix: Int = default) where S : StringProtocol初始化程序将您的二进制字符串转换为整数,并将其用作访问字母数组元素的索引。 您还可以使用String(_ value:, radix:, uppercase:)初始化程序将数组索引转换为二进制:

let alphabeth = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
var bins: [String] = []
for index in alphabeth.indices {
    let binary = String(index, radix: 2)
    let zeros = repeatElement("0", count: 5 - binary.count).joined()
    bins.append("\(zeros)\(binary)")
    print(alphabeth[index], bins.last!)
}
for binary in bins {
    if let index = Int(binary, radix: 2) {
        print(alphabeth[index])
    }   
}

正如Leo在回答的第一句话中所说,可以在Swift中将二进制字符串转换为字符,如下所示:

let alphabetArray=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "", "", "", "", "", ""]
let string = "00010"
let c = alphabetArray[Int(string, radix: 2)!]

或者,您可以使用以下方法完全绕过alphabetArray

let c = Character(Unicode.Scalar(Int(string, radix: 2)! + 97)!)
 const getChar = num => alphabeth_array[parseInt(num, 2)];

暂无
暂无

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

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