X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Fcommon%2Fsrdhelper%2Fmod.py;h=e37345a0b34f1a416c5c6b407cc746ad9b80ae4c;hp=4871205f08261aad2786a175ee2fe48687f5941a;hb=c7172e5fa77d5cd56f206c4991762ce33ef78eba;hpb=4539e9ca58966ce3c9cad4801b16c315e86ace01 diff --git a/decoders/common/srdhelper/mod.py b/decoders/common/srdhelper/mod.py index 4871205..e37345a 100644 --- a/decoders/common/srdhelper/mod.py +++ b/decoders/common/srdhelper/mod.py @@ -20,3 +20,17 @@ # Return the specified BCD number (max. 8 bits) as integer. def bcd2int(b): return (b & 0x0f) + ((b >> 4) * 10) + +def bin2int(s: str): + return int('0b' + s, 2) + +def bitpack(bits): + return sum([b << i for i, b in enumerate(bits)]) + +def bitunpack(num, minbits=0): + res = [] + while num or minbits > 0: + res.append(num & 1) + num >>= 1 + minbits -= 1 + return tuple(res)