在Python中从字符串获取x个最低有效位 [英] Get the x Least Significant Bits from a String in Python

查看:464
本文介绍了在Python中从字符串获取x个最低有效位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Python中的字符串(str)中获取x个LSB?
在具体的我有一个256位字符串组成32个字符,每个占用1个字节,从我必须得到一个char字符串与50个最低有效位。

mystr 保存我的字符串 $>

  [-7])&(3))] 
result2 = chrlist [-6:]
return.join(result1 + result2)

此函数接受 mystr 的-7rd字符的2个LSB(这些是50 LSB)
然后取出 mystr 的最后6个字符(这些是50 LSB的48个LSB)



如果我错误,请让我知道。


How can I get the x LSBs from a string (str) in Python? In the specific I have a 256 bits string consisting in 32 chars each occupying 1 byte, from wich i have to get a "char" string with the 50 Least Significant Bits.

解决方案

I think that a possible answer could be in this function: mystr holds my string

def get_lsbs_str(mystr):
    chrlist = list(mystr)
    result1 = [chr(ord(chrlist[-7])&(3))]
    result2 = chrlist[-6:]
    return "".join(result1 + result2)

this function take 2 LSBs of the -7rd char of mystr (these are the 2 MSBs of the 50 LSBs) then take the last 6 characters of mystr (these are the 48 LSB of the 50 LSB)

Please make me know if I am in error.

这篇关于在Python中从字符串获取x个最低有效位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆