使用Swift生成一个整数二进制表示? [英] Generate an integer binary representation using Swift?

查看:190
本文介绍了使用Swift生成一个整数二进制表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在swift
中创建一个接受任何Int或Uint类型的函数(并且计算关于param类型的位数)

解决方案

字符串有构造函数

  init< T :_SignedIntegerType>(_ v:T,radix:Int,uppercase:Bool = default)
init< T:_UnsignedIntegerType>(_ v:T,radix:Int,uppercase:Bool = / code>

可以在这里使用:

  let num = 100 
let str = String(num,radix:2)
print(str)
//输出:1100100


How to create a function accepting any type of Int or Uint in swift (and calculating number of bits needs regarding param type)

解决方案

String has constructors

init<T : _SignedIntegerType>(_ v: T, radix: Int, uppercase: Bool = default)
init<T : _UnsignedIntegerType>(_ v: T, radix: Int, uppercase: Bool = default)

which can be used here:

let num = 100
let str = String(num, radix: 2)
print(str)
// Output: 1100100

这篇关于使用Swift生成一个整数二进制表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆