如何将十六进制字符串转换为UInt16? [英] How to convert HEX string to UInt16?

查看:967
本文介绍了如何将十六进制字符串转换为UInt16?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个十六进制字符串d285,我想将其转换为UInt16,请指导我如何进行转换.我尝试过

I have a HEX string d285 and I want to convert it UInt16, please guide me how I can convert it. I tried this

let buffer = UInt16("\(UInt8(text, radix: 16)!)")
return Data(bytes: (buffer?.bigEndian.toBytes)!)

但是它不起作用

推荐答案

UInt16具有

UInt16 has an initializer that takes a string and radix value. This can be used to create UInt16 from string.

let hexString = "d285"
let hexToInt = UInt16(hexString, radix: 16) // prints 53893

这篇关于如何将十六进制字符串转换为UInt16?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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