Python中的右转

时间:2016-11-01 06:12:09

标签: python python-3.x

想要获得以下java代码的等效python转换。

msg = "any text"
int len = msg == null ? 0 : msg.length ();
if (len > 0) {
    byte lb[] = new byte[4];
    lb[3] = (byte) (len & 0xFF);
    lb[2] = (byte) ((len >> 8) & 0xFF);
    lb[1] = (byte) ((len >> 16) & 0xFF);
    lb[0] = (byte) ((len >> 24) & 0xFF); 
}

请帮忙。

2 个答案:

答案 0 :(得分:0)

msg   = "any text"
length = 0 if msg is None else len(msg)
if length > 0:
    lb    = []
    lb.append((length >> 24) & 0xFF)
    lb.append((length >> 16) & 0xFF)
    lb.append((length >> 8) & 0xFF)
    lb.append((length     ) & 0xFF)

newmsg = b''.join(chr(i) for i in lb)

答案 1 :(得分:0)

msg = "any text"
len =  len(msg)
lb = []
if len > 0 :
    lb.append(len & 0xFF)
    lb.append((len >> 8) & 0xFF)
    lb.append((len >> 16) & 0xFF)
    lb.append((len >> 24) & 0xFF)


for num in reversed(lb):
    print (num)