Showing posts with label binary. Show all posts
Showing posts with label binary. Show all posts

20 January 2022

python3 encode - bytes - hex -fromhex - decode

[string] encode() >>  [bytes] 

[bytes] hex() >>  [hex_string] 

[hex_string] fromhex() >> [bytes] 

[bytes] decode() >> [string]


1) bytes dan encode()

k = bytes("4d", "utf-8")

setara dengan

j = "4d".encode()

>> k = j = b"4d"



2) hex()

k = bytes("a4d", "utf-8")
>  k = b'a4d'

m = k.hex()
> m = '613464'


3) fromhex() dan decode()

n = bytes.fromhex(m)
> n = b'a4d'

p = n.decode("utf-8")
> p = 'a4d'





UTF Notes:

 encoded

j = "4d".encode("utf-8")        

>> j = b'4d'

j = "4d".encode("utf-16")

>> j = b'\xff\xfe4\x00d\x00'



18 June 2012

python binary


2 baris berikut, sama:
packed_data = binascii.unhexlify('0100000061620000cdcc2c40')
packed_data = '0100000061620000cdcc2c40'.decode('hex')

19 January 2012

Python hex -> int -> str

import binascii

x=11
print x        #>>  11
print int(x)    #>>  11
print hex(x)      #>> 0xb
print str(x)    #>>  11

x="11"
print x        #>>  11
print int(x)    #>>  11
print hex(x)      #>> ERROR
print str(x)    #>>  11

x=0x11
print x           #>> 17
print int(x)     #>> 17
print hex(x)      #>> 0x11
print str(x)      #>> 17

x = "0x17"
print x        #>> 0x17
print int(x)     #>> ERROR
print hex(x)   #>> ERROR  hex(int)
print str(x)    #>> "0x17"

x="11"
print int(x)        #>>        11
print int(x,2)        #>>        3
print int(x,8)        #>>        9
print int(x,10)    #>>        11
print int(x,16)    #>>        17

x=11
print int(x)        #>>        11
print int(x,2)        #>>  ERROR
print int(x,8)        #>>  ERROR
print int(x,10)    #>>  ERROR
print int(x,16)    #>>  ERROR

x="48454c4C61"
print binascii.unhexlify(x)  #>>    HELLa

x='a'
print ord(x)        #>> 97
print hex(ord(x))    #>> 0x61

26 April 2011

bit, byte, word

1nible = 4 bit
1byte = 8 bit
1word = 2 byte = 16 bit
1dword = 4byte = 32 bit


ps:/ setiap alamat dalam memory boleh isi 8 bit