Python内置函数进制转换的用法.doc_第1页
Python内置函数进制转换的用法.doc_第2页
Python内置函数进制转换的用法.doc_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

使用Python内置函数:bin()、oct()、int()、hex()可实现进制转换。先看Python官方文档中对这几个内置函数的描述:bin(x)Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an _index_() method that returns an integer.oct(x)Convert an integer number to an octal string. The result is a valid Python expression. If x is not a Python int object, it has to define an _index_() method that returns an (number | string, base)Convert a number or string to an integer. If no arguments are given, return 0. If a number is given, return number._int_(). Conversion of floating point numbers to integers truncates towards zero. A string must be a base-radix integer literal optionally preceded by + or - (with no space in between) and optionally surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that int(010, 0) is not legal, while int(010) is, as well as int(010, 8).hex(x)Convert an integer number to a hexadecimal string. The result is a valid Python expression. If x is not a Python int object, it has to define an _index_() method that returns an integer.2进制8进制10进制16进制2进制-bin(int(x, 8)bin(int(x, 10)bin(int(x, 16)8进制oct(int(x, 2)-oct(int(x, 10)oct(int(x, 16)10进制int(x, 2)int(x, 8)-int(x, 16)16进制hex(int(x, 2)hex(int(x, 8)hex(int(x, 10)-bin()、oct()、hex()的返回值均为字符串,且分别带有0b、0o、0x前缀。Python进制转换(二进制、十进制和十六进制)实例#!/usr/bin/env python# -*- coding: utf-8 -*-# 2/10/16 base trans. wrote by srcdog on 20th, April, 2009# ld elements in base 2, 10, 16.import os,sys# global definition# base = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, Fbase = str(x) for x in range(10) + chr(x) for x in range(ord(A),ord(A)+6)# bin2dec# 二进制 to 十进制: int(str,n=10) def bin2dec(string_num): return str(int(string_num, 2)# hex2dec# 十六进制 to 十进制def hex2dec(string_num): return str(int(string_num.upper(), 16)# dec2bin# 十进制 to 二进制: bin() def dec2bin(string_num): num = int(string_num) mid = while True: if num = 0: break num,rem = divmod(num, 2) mid.append(baserem) return .join(str(x) for x in mid:-1)# dec2hex# 十进制 to 八进制: oct() # 十进制 to 十六进制: hex() def dec2hex(string_num): num = int(string_num) mid = while True: if num = 0: break num,rem = divmod(num, 16) mid.append(baserem) return .join(str(x) for x in mid:-1)# hex2tobin# 十六进制 to 二进制: bin(int(str,16) def hex2bin(string_num):

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论