Data to Information to Knowledge to Wisdom
This is the capital of new Alchemy

十進位,二進位,八進位,十六進位在python

在稍微複習自己的電概筆記時,心血來潮的想用python來算算看。
在用google找了一下關鍵字,找了一下。(題外話,發現其實很多網站的文章都是從python的本家複製過來的。本家果然是python的聖地,萬劍歸宗的地方。irc則是高手暗伏的深淵^_^)

anyway,試了一下,還蠻簡單的,python的oo架構真是方便。不過,我還沒找到如何使用有小數的換算(直接用hex()等會造成syntax error>_<)。或許是因為除了老師出作業外,這種換算很少用吧。當然,也有可能是我笨笨,找不到。

"4.73. How do I specify hexadecimal and octal integers?
To specify an octal digit, precede the octal value with a zero. For example, to set the variable "a" to the octal value "10" (8 in decimal), type:

>>> a = 010

To verify that this works, you can type "a" and hit enter while in the interpreter, which will cause Python to spit out the current value of "a" in decimal:

>>> a
8

Hexadecimal is just as easy. Simply precede the hexadecimal number with a zero, and then a lower or uppercase "x". Hexadecimal digits can be specified in lower or uppercase. For example, in the Python interpreter:

>>> a = 0xa5
>>> a
165
>>> b = 0XB2
>>> b
178

To convert back, use the hex() and oct() builtin functions:

>>> oct( emoticon
'010'
>>> hex(17 emoticon
'0xb2'"

Python FAQ Entry
palmpilote在2003年10月07日23時24分03秒時貼了這一篇 - Category: computer

TrackBack

Trackback
這篇文章目前沒有任何的ping
請利用這個TrackBack網址 來ping這篇文章

回應

沒有任何回應

新增回應

:

:
: