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

时间:2018-01-08 15:49:03

标签: javascript swift string binary

我创建了一个简单的函数,可以将每个字母字符(a-z)转换为 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 - 哪种语言无关紧要 !)

4 个答案:

答案 0 :(得分:1)

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

&#13;
&#13;
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
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以使用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])
    }   
}

答案 2 :(得分:1)

正如Leo在his answer的第一句中所说,可以将该二进制字符串转换为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)!)

答案 3 :(得分:0)

 const getChar = num => alphabeth_array[parseInt(num, 2)];