在Ruby中将整数转换为十六进制字符串 [英] Converting an integer to a hexadecimal string in Ruby

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

问题描述

有没有一种内置的方式可以将Ruby中的整数转换为十六进制等价的?



类似于 String#to_i

 0A.to_i(16)#=> 10 

也许:

 0A.hex#=> 10 

我知道如何推出自己的产品,但使用内建的Ruby功能可能更有效率。 Fixnum.html#method-i-to_srel =noreferrer> to_s 除10之外的基础:

  10.to_s(16)#=>> a


Is there a built in way to convert an integer in Ruby into its hexadecimal equivalent?

Something like the opposite of String#to_i:

"0A".to_i(16) #=>10

Like perhaps:

"0A".hex #=>10

I know how to roll my own, but it's probably more efficient to use a built in Ruby function.

解决方案

You can give to_s a base other than 10:

10.to_s(16)  #=> "a"

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

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