How to convert from bytearray/bytes in micropython?

Question:

I’m hashing things with uhashlib in micropython on the pi pico. Here’s an example:

import sys
import os
import uhashlib
import time

time_now = "blergh"
hash_test = uhashlib.sha256(time_now).digest()

print(time_now)
print(hash_test)

This outputs:

blergh
b'Y|x84Wxa1x1dx86cb~x0bLx1e\x92xcd-x93x05xddzx0exe1x9fx9axc1H6x93xd8x0c8'

…which, clearly, isn’t super useful. I’ve tried a bunch of things, but I can’t figure out how to convert from the bytes (b'...') in micropython. Anyone have ideas/methods?

Asked By: syzygetic

||

Answers:

You should be able to directly decode any bytes to hex with the .hex() method on it!

>>> b"blerg".hex()
'626c657267'

I don’t have uhashlib, but this works with the stock hashlib!

>>> hashlib.sha256(b"blergh").digest().hex()
'597c8457a11d8663627e0b4c1e5c92cd2d9305dd7a0ee19f9ac1483693d80c38'
>>> hashlib.sha256(b"blergh").hexdigest()
'597c8457a11d8663627e0b4c1e5c92cd2d9305dd7a0ee19f9ac1483693d80c38'
Answered By: ti7

Use ubinascii.hexlify and jump through hoops.

ubinascii.hexlify() returns bytes. By decoding the bytes to a str and then converting that str to an int (with base16), we can then pass the value to hex(). There is no hex attribute for bytes in micropython.

The below has been fully tested on the Raspberry Pi Pico running micropython 1.14. I suspect earlier versions would also work, as long as they possess both of the module dependencies.

import ubinascii, uhashlib

hs = uhashlib.sha256(b'blergh')

def hexdigest(sha):
    return hex(int(ubinascii.hexlify(sha.digest()).decode(), 16))

hx = hexdigest(hs) #0x597c8457a11d8663627e0b4c1e5c92cd2d9305dd7a0ee19f9ac1483693d80c38

Answered By: OneMadGypsy

this mpython script has a code block that converts bytearray into decimal ascii How do you import that library if it’s offline? I mean what is that library do specifically? Is there anyway you can put that in longhand?
it took many months of diligence To discover how to translate these libraries into longhand. But I can assure you that they have absolutely no external commands that must be imported or downloaded from any repository or library. I’m kinda proud of it.
[https://wokwi.com/projects/376341312102027265][https://wokwi.com/projects/376341312102027265]

puter=bytearray(1)
while 1:
  k=machine.Pin(28).value()
  if (k>b):
    k1=k1+1-4*(k1>3)
  b=k
  k2=machine.Pin(27).value()
  if (k2>b2):
    k1=k1+1-4*(k1>3)
  b2=k2
  rvdc=machine.ADC(26).read_u16()
  if ttt==t:
    machine.lightsleep(100)
  if ttt!=t:
    ttt=t
    s=str(int((t/60-t//60)*60));m=str(int((t/3600-t//3600)*60));h=str(int((t/86400-t//86400)*24));d=str(t//86400);w=str((t//86400*7))    # Extrapolate seconds hours minutes and days from raw uptime seconds
    tt=("00"+h)[-2:]+("00"+m)[-2:]+("00"+s)[-2:]+("00"+d)[-2:]+"77" # compile the output buffer 
    for nn in range(8):
      n=int(tt[nn])
      machine.SPI(0,baudrate=32768,sck=machine.Pin(2),mosi=machine.Pin(3),miso=machine.Pin(4)).readinto(puter,int(ssebr[n*8:n*8+8],2))
      chrout=str(puter)[12]
      if chrout==chr(92):
        print(ord(chr(int("0x"+(str(puter)[14:16])))),"$",end="")
      if chrout!=chr(92):
        print(ord(str(puter)[12]),"@",end="")
    machine.Pin(0).value(0)    # low latched the data into the display
    machine.Pin(0).value(1)    # high latched the data into the display
    print("")
Answered By: Jonathan James