将双十进制转换为十六进制,二进制和八进制字符串 [英] Convert double decimal to Hex, Binary and Octal strings

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

问题描述

是否有可用于目标c的自定义或标准库,可将十进制浮点值转换为十六进制,二进制和八进制NSString?

Is there a custom or standard library that I can use with objective c to convert a decimal floating point value into Hexadecimal, Binary and Octal NSStrings?

我一直在四处查看,但只能找到使用以下方法将其他方向(从其他所有方向转换为十进制)的方法:

I've been looking around a lot but can only find how to convert the other direction (from everything else into decimal), by using: double result = (double)strtoll(myHexString.UTF8String, NULL,16)

推荐答案

对于八进制和十六进制,您可以利用以下事实:可以使用%x和%o指定符将数字格式化为十六进制和八进制:

As for octal and hex, you can exploit the fact that you can format numbers in hex and octal using the %x and %o specifiers:

NSString* str=[NSString stringWithFormat: @"Hex: %x Octal: %o",14,14];

但这仅用于整数部分,因此您可以使用整数.如果您还想要小数部分,则需要实现算法,这与二进制相同.

But this only for the integral part, so you can use integers.If you want also the decimal part, to obtain fixed points you need to implement the algorithm, same for binary.

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

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