Python                                                          Home : www.sharetechnote.com

Python - Number Manipulation

NOTE 1 : All the examples in this page are written in Python 3.x. It may not work if you use Pyton 2.x

NOTE 2 : All the examples in this page are assumed to be written/run on Windows 7 unless specifically mentioned. You MAY (or may not) need to modify the syntax a little bit if you are running on other operating system.

• Convert a number to a string - Example 1
• Convert a ASCII character into a Byte Array
• Convert a Hex String into a stream of Hex Numbers (binascii package) - Example 3
• Convert a stream of Hex Numbers into a Byte Array (bytearray,binascii) - Example 4
• Convert a ByteArray into a String of Hex Numbers (bytearray,binascii)
• Convert a ByteArray into a Long Integer and convert it into Binary - Example 6
• Convert a ByteArray into a Long Integer and convert it into Binary and Back to Integer- Example 7
• Formating a Number - '{format}'.format(number) - Example 8

Examples :

 < Example 1 > Convert a number to a string   print("str(123)=",str(123)) print("str(12345678901234567890)=",str(12345678901234567890)) print("str(0xA)=",str(0xA)) print("str(0xAB)=",str(0xAB)) print("str(0xABC)=",str(0xABC)) print("str(0b1)=",str(0b1)) print("str(0b0111)=",str(0b0111)) print("str(0b11111111)=",str(0b11111111))     Result :---------------------------------- str(123)= 123 str(12345678901234567890)= 12345678901234567890 str(0xA)= 10 str(0xAB)= 171 str(0xABC)= 2748 str(0b1)= 1 str(0b0111)= 7 str(0b11111111)= 255     < Example 2 > Convert a ASCII character into a Byte Array   numStr = "123ABCDEF" bArray = bytearray(numStr.encode('ascii')) print(bArray)   for b in numStr :    print("elements in numStr :", b)   for b in bArray :    print("elements in bArray :", b)     Result :---------------------------------- bytearray(b'123ABCDEF') elements in numStr : 1 elements in numStr : 2 elements in numStr : 3 elements in numStr : A elements in numStr : B elements in numStr : C elements in numStr : D elements in numStr : E elements in numStr : F elements in bArray : 49 elements in bArray : 50 elements in bArray : 51 elements in bArray : 65 elements in bArray : 66 elements in bArray : 67 elements in bArray : 68 elements in bArray : 69 elements in bArray : 70     < Example 3 > Convert a Hex String into a stream of Hex Numbers (binascii package)   import binascii   hexStr = "AABBCC" hexNumbers = binascii.a2b_hex(hexStr)   print("hexStr = ",hexStr) print("hexNumbers = ", hexNumbers)   Result :---------------------------------- hexStr =  AABBCC hexNumbers =  b'\xaa\xbb\xcc'     < Example 4 > Convert a stream of Hex Numbers into a Byte Array (binarray,binascii)   import binascii   hexStr = "AABBCC" hexNumbers = binascii.a2b_hex(hexStr) byteArray = bytearray(hexNumbers)   print("hexStr = ",hexStr) print("hexNumbers = ", hexNumbers) print("byteArray = ", byteArray) print("hexStr[0]=",hexStr[0]) print("hexNumbers[0]=",hexNumbers[0]) print("byteArray[0]=",byteArray[0])   Result :---------------------------------- hexStr =  AABBCC hexNumbers =  b'\xaa\xbb\xcc' byteArray =  bytearray(b'\xaa\xbb\xcc') hexStr[0]= A hexNumbers[0]= 170 byteArray[0]= 170     < Example 5 >  Convert a ByteArray into a String of Hex Numbers (bytearray,binascii)   import binascii   ByteAry = b'\x45\x00\x00\x16\x36\xdd\x00\x00\x80\xfd\xfe\xa7\x01\x01\x01\x64\x01\x01\x01\x01\x48\x69' HexStr=binascii.b2a_hex(ByteAry)   print("Byte Array = ",ByteAry) print("Hex String = ",HexStr) print("Ascii String = ",HexStr.decode('ascii'))   Result :----------------------------------   Byte Array =  b'E\x00\x00\x166\xdd\x00\x00\x80\xfd\xfe\xa7\x01\x01\x01d\x01\x01\x01\x01Hi' Hex String =  b'4500001636dd000080fdfea701010164010101014869' Ascii String =  4500001636dd000080fdfea701010164010101014869     < Example 6 > Convert a ByteArray into a Long Integer and convert it into Binary   import binascii   ByteAry = b'\x45\x00\x00\x16\x36' BigInteger = int.from_bytes(ByteAry[0:len(ByteAry)], 'big') BigBinary = '{0:0{1}b}'.format(BigInteger,len(ByteAry*8))   print("Byte Array = ",ByteAry) print("BigInteger = ",BigInteger) print("BigInteger (Hex) = ",'{0:X}'.format(BigInteger)) print("BigBinary = ",BigBinary)   Result :----------------------------------   Byte Array =  b'E\x00\x00\x166' Big Integer =  296352749110 Big Integer (Hex) =  4500001636 BigBinary =  0100010100000000000000000001011000110110     < Example 7 >  Convert a ByteArray into a Long Integer and convert it into Binary and Back to Integer   import binascii   ByteAry = b'\x45\x00\x00\x16\x36' BigInteger = int.from_bytes(ByteAry[0:len(ByteAry)], 'big') BigBinary = '{0:0{1}b}'.format(BigInteger,len(ByteAry*8)) BigIntFromBinary = int(BigBinary,2)   print("Byte Array = ",ByteAry) print("BigInteger = ",BigInteger) print("BigInteger (Hex) = ",'{0:X}'.format(BigInteger)) print("BigBinary = ",BigBinary) print("BigIntFromBinary = ",BigIntFromBinary) print("BigIntFromBinary (Hex) = ",'{0:X}'.format(BigIntFromBinary))   Result :----------------------------------   Byte Array =  b'E\x00\x00\x166' BigInteger =  296352749110 BigInteger (Hex) =  4500001636 BigBinary =  0100010100000000000000000001011000110110 BigIntFromBinary =  296352749110 BigIntFromBinary (Hex) =  4500001636     < Example 8 >   print("Decimal Format=================================") print("'{:d}'.format(123)=",'{:d}'.format(123)) print("'{:8d}'.format(123)=",'{:8d}'.format(123)) print("'{:08d}'.format(123)=",'{:08d}'.format(123)) print("'{0:d}'.format(123)=",'{0:d}'.format(123)) print("'{0:8d}'.format(123)=",'{0:8d}'.format(123)) print("'{0:08d}'.format(123)=",'{0:08d}'.format(123))   print("Float Format===================================") print("'{:f}'.format(123)=",'{:f}'.format(123)) print("'{:5.1f}'.format(123)=",'{:5.1f}'.format(123)) print("'{:8.5f}'.format(123)=",'{:8.5f}'.format(123)) print("'{:10.5f}'.format(123)=",'{:10.5f}'.format(123)) print("'{:010.5f}'.format(123)=",'{:010.5f}'.format(123))   print("Hex Format====================================") print("'{:x}'.format(123)=",'{:x}'.format(123)) print("'{:8x}'.format(123)=",'{:8x}'.format(123)) print("'{:08x}'.format(123)=",'{:08x}'.format(123)) print("'{:#08x}'.format(123)=",'{:#08x}'.format(123)) print("'{0:x}'.format(123)=",'{0:x}'.format(123)) print("'{0:8x}'.format(123)=",'{0:8x}'.format(123)) print("'{0:08x}'.format(123)=",'{0:08x}'.format(123)) print("'{0:#08x}'.format(123)=",'{0:#08x}'.format(123))   print("'{:X}'.format(123)=",'{:X}'.format(123)) print("'{:8X}'.format(123)=",'{:8X}'.format(123)) print("'{:08X}'.format(123)=",'{:08X}'.format(123)) print("'{:#08X}'.format(123)=",'{:#08X}'.format(123)) print("'{0:X}'.format(123)=",'{0:X}'.format(123)) print("'{0:8X}'.format(123)=",'{0:8X}'.format(123)) print("'{0:08X}'.format(123)=",'{0:08X}'.format(123)) print("'{0:#08X}'.format(123)=",'{0:#08X}'.format(123))   print("Binary Format==================================") print("'{:b}'.format(123)=",'{:b}'.format(123)) print("'{:16b}'.format(123)=",'{:16b}'.format(123)) print("'{:016b}'.format(123)=",'{:016b}'.format(123)) print("'{:#016b}'.format(123)=",'{:#016b}'.format(123))   print("Format with digit specification ===============") print("#{1} means the second argument in format() which is 8 in this example") print("'{0:{1}d}'.format(123,8)=",'{0:{1}d}'.format(123,8)) print("#{1} means the second argument in format() which is 16 in this example") print("'{0:{1}d}'.format(123.16)=",'{0:{1}d}'.format(123,16)) print("#{1} means the second argument in format() which is 8 in this example") print("'{0:{1}b}'.format(123,8)=",'{0:{1}b}'.format(123,8)) print("#{1} means the second argument in format() which is 16 in this example") print("'{0:{1}b}'.format(123.16)=",'{0:{1}b}'.format(123,16))   print("Format with digit and base specification ======") print("#{1} means the second argument in format() which is 8 in this example") print("#{2} means the third argument in format() which is 'd' in this example") print("'{0:{1}{2}}'.format(123,8)=",'{0:{1}{2}}'.format(123,8,'d')) print("#{1} means the second argument in format() which is 16 in this example") print("#{2} means the third argument in format() which is 'd' in this example") print("'{0:{1}{2}}'.format(123.16)=",'{0:{1}{2}}'.format(123,16,'d')) print("#{1} means the second argument in format() which is 16 in this example") print("#{2} means the third argument in format() which is 'd' in this example") print("'{0:0{1}{2}}'.format(123.16)=",'{0:0{1}{2}}'.format(123,16,'d')) print("#{1} means the second argument in format() which is 8 in this example") print("#{2} means the third argument in format() which is 'b' in this example") print("'{0:{1}{2}}'.format(123,8)=",'{0:{1}{2}}'.format(123,8,'b')) print("#{1} means the second argument in format() which is 16 in this example") print("#{2} means the third argument in format() which is 'b' in this example") print("'{0:{1}{2}}'.format(123.16)=",'{0:{1}{2}}'.format(123,16,'b')) print("#{1} means the second argument in format() which is 16 in this example") print("#{2} means the third argument in format() which is 'b' in this example") print("'{0:0{1}{2}}'.format(123.16)=",'{0:0{1}{2}}'.format(123,16,'b'))     Result :---------------------------------- Decimal Format================================= '{:d}'.format(123)= 123 '{:8d}'.format(123)=      123 '{:08d}'.format(123)= 00000123 '{0:d}'.format(123)= 123 '{0:8d}'.format(123)=      123 '{0:08d}'.format(123)= 00000123 Float Format=================================== '{:f}'.format(123)= 123.000000 '{:5.1f}'.format(123)= 123.0 '{:8.5f}'.format(123)= 123.00000 '{:10.5f}'.format(123)=  123.00000 '{:010.5f}'.format(123)= 0123.00000 Hex Format==================================== '{:x}'.format(123)= 7b '{:8x}'.format(123)=       7b '{:08x}'.format(123)= 0000007b '{:#08x}'.format(123)= 0x00007b '{0:x}'.format(123)= 7b '{0:8x}'.format(123)=       7b '{0:08x}'.format(123)= 0000007b '{0:#08x}'.format(123)= 0x00007b '{:X}'.format(123)= 7B '{:8X}'.format(123)=       7B '{:08X}'.format(123)= 0000007B '{:#08X}'.format(123)= 0X00007B '{0:X}'.format(123)= 7B '{0:8X}'.format(123)=       7B '{0:08X}'.format(123)= 0000007B '{0:#08X}'.format(123)= 0X00007B Binary Format================================== '{:b}'.format(123)= 1111011 '{:16b}'.format(123)=          1111011 '{:016b}'.format(123)= 0000000001111011 '{:#016b}'.format(123)= 0b00000001111011 Format with digit specification =============== #{1} means the second argument in format() which is 8 in this example '{0:{1}d}'.format(123,8)=      123 #{1} means the second argument in format() which is 16 in this example '{0:{1}d}'.format(123.16)=              123 #{1} means the second argument in format() which is 8 in this example '{0:{1}b}'.format(123,8)=  1111011 #{1} means the second argument in format() which is 16 in this example '{0:{1}b}'.format(123.16)=          1111011 Format with digit and base specification ====== #{1} means the second argument in format() which is 8 in this example #{2} means the third argument in format() which is 'd' in this example '{0:{1}{2}}'.format(123,8)=      123 #{1} means the second argument in format() which is 16 in this example #{2} means the third argument in format() which is 'd' in this example '{0:{1}{2}}'.format(123.16)=              123 #{1} means the second argument in format() which is 16 in this example #{2} means the third argument in format() which is 'd' in this example '{0:0{1}{2}}'.format(123.16)= 0000000000000123 #{1} means the second argument in format() which is 8 in this example #{2} means the third argument in format() which is 'b' in this example '{0:{1}{2}}'.format(123,8)=  1111011 #{1} means the second argument in format() which is 16 in this example #{2} means the third argument in format() which is 'b' in this example '{0:{1}{2}}'.format(123.16)=          1111011 #{1} means the second argument in format() which is 16 in this example #{2} means the third argument in format() which is 'b' in this example '{0:0{1}{2}}'.format(123.16)= 0000000001111011   < Example 19 >     < Example 19 >