python程式設計中,怎樣把小於16的十進位制數字轉化成位元組的十六進位製數

2021-04-17 17:36:26 字數 2606 閱讀 3044

1樓:一豬之哀傷一

既然是小於16的數字,那前面的那個0又是怎麼來的呢???

如果單純是想補成兩位的話

>>> '%02x' % 1

'01'

2樓:

>>> a=1

>>> '%02x'%a

'01'

>>> a=17

>>> '%02x'%a

'11'

>>>

python 如何將16進製制資料字串去掉0x部分,然後轉換成16進製制資料寫入到檔案中

3樓:匿名使用者

# -*- coding: utf-8 -*-__author__ = 'lpe234'

__date__ = '2015-04-01'

hex_list = ['0xaa', '0xed', '0xef', '0xde']

f = file('x.txt', 'a+')for x in hex_list:

if x.startswith('0x'):

x = x[2:]

print x

f.write(x+'\n')

f.close()

輸出檔案:

aaed

efde

4樓:糖糖寳寳

一般計算機的十六進位製數直接輸出的時候是

不補0的,所以0x12e 就是 0x0012e,就好像 0005和5在整型數是儲存成一樣的值。

a='0x0012e'

b= hex(eval(a))

print b

輸出0x12e

5樓:匿名使用者

例如你要轉

換的數字是50,追加寫到num.txt裡面file_object = open('num.txt','w+')x = hex(50)

x = x[2:]

file_object.write(x)

file_object.close( )

python如何將整數轉化成二進位制字串

6樓:she小於

首先你可以自己寫函式採用%2的方式來算.

但是python自帶了方法 bin.

比如bin(12345)回返回字串'0b11000000111001' 這個時候在把0b去掉即可.

bin(number).replace('0b','')

7樓:匿名使用者

直接上**:

#coding=gbk

def intto2str( x , k ):

""" intto2str( x , k )將整數 x 轉化為 k位2進製字串

"""try:

x = long( x)

except:

x = 0

try:

k = int( k)

except:

k = 0

if k<1 :

k = 1

if x<0 :

fh = 1 ; x = -x

else:

fh = 0

a =[ 0 for j in xrange( 0, k ) ]j = k-1

while (j>=0) and ( x>0):

y = x % 2

x = x / 2

a[ j ] = y

j = j - 1

if fh==1:

# 求反

for j in xrange( 0, k):

if a[j] ==1 :

a[j] = 0

else:

a[j] = 1

# 末位加1

j = k - 1

while j>=0:

a[j] = a[j] +1

if a[j]<=1:

break;

a[j] = 0

j = j -1

return "".join([ chr(j+48) for j in a ])

print intto2str( 8, 8 )   # 應顯示 00001000

print intto2str( -1, 8 )   # 應顯示 10000000

8樓:家庭教師張

[python] view plain copylast_mask_str = '11110000'

last_mask_str = str(int(last_mask_str, 2))

print last_mask_str

#240

print bin(int(last_mask_str))#0b11110000

9樓:

>>> num = 1396940819.29708>>> bin(int(num))

'0b1010011010000111010000000010011'

>>>

10樓:匿名使用者

''.format(128) # python 3.x 以上

在python核心程式設計第二版中的例題不明白的點,求解答下圖紅線圈出來的,不明白

請聽我重複,三 個引號並不是註釋,三個引號並不是註釋,三個引號並回不是註釋。重要的話說三遍。其 答實python中三個引號 三個單引號或者三個雙引號 的作用是可換行的字串,是字串,並不指的是註釋,在python中註釋只有一種方法那就是 但是三引號可以用來作為註釋,是因為他定義了乙個字串,而這個字串並...

python中怎樣使用shape計算矩陣的行和列

import numpy a numpy.array 1,2,3 4,5,6 print a.shape 矩陣有乙個shape屬性,是乙個 行,列 形式的元組 python中怎樣使用shape計算矩陣的行和列 import numpy a numpy.array 1,2,3 4,5,6 print ...

Excel中如何做到如果數字小於1的時候不管小數是多少都進

if a1 1,roundup a1,0 round a1,0 小於1向上取整,大於1四捨五入。excel單元格中如果大於0小於1,則顯示1,如果大於1,則顯示原來的數 根據你的舉例說明copy,我理解你的需求就bai是du 如果有小數的,就 zhi把小數進製,只顯示整數。dao 有三個函式可以實現...