如何将数字转换为十六进制数? [英] How to convert a number to hex number?

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

问题描述

我尝试这个命令:

import string
string.atoi('''78'',16)





这是120而不是4E。


有人可以分辨我如何将十进制数转换为十六进制数?可以

打印A,B,C,DEF。

谢谢。

解决方案

嗨!


尝试hex:

hex(120)
''0x78''


考虑转换字符串 - > int使用int() - function:print int .__ doc __



int(x [,base]) - >整数


如果可能,将字符串或数字转换为整数。一个浮点

参数将被截断为零(这不包括字符串

表示浮点数!)转换字符串时,使用

可选基数。转换

非字符串时提供基数是错误的。如果参数超出整数范围,则会返回一个长对象




Daniel


< blockquote> Hako写道:

我尝试这个命令:

import string
string.atoi('''78'',16)


120
这是120而不是4E。

有人可以告诉我如何将十进制数转换为十六进制数?可以打印A,B,C,DEF。



要打印整数的十六进制字符串表示,n,使用

print hex(n)



print"%x" %n


问候,


Aaron Bingham


和如果你只想要十六进制数,请尝试:

hex(255)[2:]



''ff''


I try this command:

import string
string.atoi(''78'',16)


120
this is 120 not 4E.

Someone can tell me how to convert a decimal number to hex number? Can
print A, B, C,DEF.
Thank you.

解决方案

Hi!

Try hex:

hex(120) ''0x78''

Consider converting string -> int using the int()-function: print int.__doc__


int(x[, base]) -> integer

Convert a string or number to an integer, if possible. A floating point
argument will be truncated towards zero (this does not include a string
representation of a floating point number!) When converting a string, use
the optional base. It is an error to supply a base when converting a
non-string. If the argument is outside the integer range a long object
will be returned instead.

Daniel


Hako wrote:

I try this command:

import string
string.atoi(''78'',16)


120
this is 120 not 4E.

Someone can tell me how to convert a decimal number to hex number? Can
print A, B, C,DEF.


To print the hexidecimal string representation of an integer, n, use
print hex(n)
or
print "%x" % n

Regards,

Aaron Bingham


And if you want only the hex number, try:

hex(255)[2:]


''ff''


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

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