MicroPython Conversion

Somsak Lima
2 min readOct 3, 2024

--

Data Type มีหลายแบบ เลย ขอค่อยๆ รวมวิธีการแปลงไปแปลงกลับไว้ที่นี่ ค่อยๆ รวบรวมไปเรื่อยๆครับ

ดูวิธีเรียกชื่อตัวแปลก่อน ดูที่

0. แสดงค่า Debug

a=5
print("a=",a, " type", type(a))
>>> %run -c $EDITOR_CONTENT
a= 5 type <class 'int'>
  1. แปลง Byte String ไปเป็น ASCII String
message=b'4f6e'
message=ubinascii.unhexlify(message).decode('ascii')
print(message)

ผล

>>> %run -c $EDITOR_CONTENT
On

2. ubinascii.unhexlify, ubinascii.hexlify


>>> ubinascii.hexlify('\x11\x22123')
b'1122313233'
>>> ubinascii.hexlify('abcdfg')
b'616263646667'
>>> ubinascii.hexlify('\x11\x22123', ' ')
b'11 22 31 32 33'
>>> ubinascii.hexlify('\x11\x22123', ',')
b'11,22,31,32,33'
>>> ubinascii.unhexlify('313233')
b'123'
import ubinascii

data = b'\x01\x02\x03\x04\x05\x06\x07\x08'
hexstr = ubinascii.hexlify(data)
print(hexstr) # Output b'0102030405060708'

bytearr = ubinascii.unhexlify(hexstr)
print(bytearr) # Output b'\x01\x02\x03\x04\x05\x06\x07\x08'

ผล

>>> %run -c $EDITOR_CONTENT
b'0102030405060708'
b'\x01\x02\x03\x04\x05\x06\x07\x08'

3. ord

>>> ord('A')
65
>>> '%x' % ord('A')
'41'

4. Base64 ติดตั้ง Lib Base64 ก่อน

import base64
print( base64.b64encode( “Hello, world!”.encode( “utf-8” ) ) )

import base64
print( base64.b64encode( "Hello, world!".encode( "utf-8" ) ) )
data = "Hello, world!"
# Encode
encodedBytes = base64.b64encode(data.encode("utf-8"))
encodedStr = str(encodedBytes, "utf-8")
print("Encode text: " + encodedStr)
# Decode
print("Decode text: "+str(base64.b64decode(encodedStr),"utf-8"))

ผล

b'SGVsbG8sIHdvcmxkIQ=='
Encode text: SGVsbG8sIHdvcmxkIQ==
Decode text: Hello, world!

5. ubinascii.a2b_base64, ubinascii.b2a_base64

import ubinascii

data = b'\x01\x02\x03\x04\x05\x06\x07\x08'
base64str = ubinascii.b2a_base64(data)
print(base64str) # Output b'AQIDBAUGBwg='

bytearr = ubinascii.a2b_base64(base64str)
print(bytearr) # Output b'\x01\x02\x03\x04\x05\x06\x07\x08'

ผล

>>> %run -c $EDITOR_CONTENT
b'AQIDBAUGBwg=\n'
b'\x01\x02\x03\x04\x05\x06\x07\x08'

หรือ

import ubinascii
baseBy= ubinascii.b2a_base64("hello")
print(baseBy)
print(ubinascii.a2b_base64('aGVsbG8=\n'))

ผล

b'aGVsbG8=\n'
b'hello'

6. uhashlib

import uhashlib
import ubinascii
hash_obj = uhashlib.sha256()
hash_obj.update(b"QuecPython")
res = hash_obj.digest()
print(res)
hex_msg = ubinascii.hexlify(res)
print(hex_msg)

ผล

b"\x1e\xc6gq\xb3\xa9\xac>\xa4\xc4O\x00\x9eTW\x97\xd4.\x9e}Bo\xff\x82u\x89Th\xfe'\xc6\xcd"
b'1ec66771b3a9ac3ea4c44f009e545797d42e9e7d426fff8275895468fe27c6cd'

7. md5 sha1 sha256

import uhashlib
import ubinascii
hash_obj = uhashlib.sha256()
hash_obj.update(b"QuecPython")
res = hash_obj.digest()
hex_msg = ubinascii.hexlify(res)

import uhashlib
import ubinascii
data = b"QuecPython"
hash_obj = uhashlib.md5()
hash_obj.update(data)
res = hash_obj.digest()
hex_msg = ubinascii.hexlify(res)
print("md5:", hex_msg)

hash_obj = uhashlib.sha1()
hash_obj.update(data)
res = hash_obj.digest()
hex_msg = ubinascii.hexlify(res)
print("sha1:", hex_msg)

hash_obj = uhashlib.sha256()
hash_obj.update(data)
res = hash_obj.digest()
hex_msg = ubinascii.hexlify(res)
print("sha256:", hex_msg)

ผล

md5: b'37b8419ee7cdb3c64d7e66019216117c'
sha1: b'614a4247ef68e9f9793e11353cc86acb932badab'
sha256: b'1ec66771b3a9ac3ea4c44f009e545797d42e9e7d426fff8275895468fe27c6cd'

8.

msg=b'016700fa036864'
a=msg.decode('ascii')
print (a)
>>> %run -c $EDITOR_CONTENT
016700fa036864

9.

import binascii
mac_bytes = b'\xacg\xb27-\xcc'
mac_str = binascii.hexlify(mac_bytes).decode()
print(mac_str)
ac67b2372dcc

99.ตัวอย่างการใช้

    g=ubinascii.unhexlify(ubinascii.hexlify(c.getBuffer()).decode('ascii'))

--

--

Somsak Lima
Somsak Lima

Written by Somsak Lima

สนับสนุนและส่งเสริมให้ผู้สนใจสามารถใช้งานเทคโนโลยี LoRa และ LoRaWAN ได้ โดยนำความรู้ที่ได้ไปต่อยอดเพื่อใช้งาน

No responses yet