本文介绍了迅速.从整数获取二进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有二进制0000010,它表示为一个整数数组.从此二进制文件中,我得到一个Integer:

I have binary 0000010 which is represented as an array of ints. From this binary I get an Integer:

let number = Int32([0, 0, 0, 0, 0, 1, 0].reduce(0, combine: {$0*2 + $1}))//数字= 2

但是当我想进行逆运算以获得String时:

but when I want to inverse operation to get a String:

let binaryString = String(2, radix: 2)//binaryString = "10"

let binaryString = String(2, radix: 2) // binaryString = "10"

看来基数会削减一些位(如果它们为0),又如何返回5个零?

So seems radix cuts some bits if they are 0, how to return 5 more zeros?

推荐答案

let binaryString = String(2, radix: 2)
let other = String(count: 8 - binaryString.characters.count, repeatedValue: Character("0")) + binaryString

这篇关于迅速.从整数获取二进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 09:55