@
imn1 的确你说的没错。用于校验的话,另几种算法结果是有问题的。
---
for i in range(10):
j = str(i).encode()
i = pd.Series([i]).astype('bytes').tolist()[0]
k = binascii.crc32(j)
print(f'{type(i)} {i} -> {type(j)} {j} | {k}')
<class 'int'> 0 -> <class 'bytes'> b'0' | 4108050209
<class 'int'> 1 -> <class 'bytes'> b'1' | 2212294583
<class 'int'> 2 -> <class 'bytes'> b'2' | 450215437
<class 'int'> 3 -> <class 'bytes'> b'3' | 1842515611
<class 'int'> 4 -> <class 'bytes'> b'4' | 4088798008
<class 'int'> 5 -> <class 'bytes'> b'5' | 2226203566
<class 'int'> 6 -> <class 'bytes'> b'6' | 498629140
<class 'int'> 7 -> <class 'bytes'> b'7' | 1790921346
<class 'int'> 8 -> <class 'bytes'> b'8' | 4194326291
<class 'int'> 9 -> <class 'bytes'> b'9' | 2366072709
---
@
lxy42 j = struct.pack('i', i)
<class 'int'> 0 -> <class 'bytes'> b'\x00\x00\x00\x00' | 558161692
<class 'int'> 1 -> <class 'bytes'> b'\x01\x00\x00\x00' | 2583214201
<class 'int'> 2 -> <class 'bytes'> b'\x02\x00\x00\x00' | 2337085335
<class 'int'> 3 -> <class 'bytes'> b'\x03\x00\x00\x00' | 871461106
<class 'int'> 4 -> <class 'bytes'> b'\x04\x00\x00\x00' | 2921744459
<class 'int'> 5 -> <class 'bytes'> b'\x05\x00\x00\x00' | 379203374
<class 'int'> 6 -> <class 'bytes'> b'\x06\x00\x00\x00' | 70222016
<class 'int'> 7 -> <class 'bytes'> b'\x07\x00\x00\x00' | 3163809701
<class 'int'> 8 -> <class 'bytes'> b'\x08\x00\x00\x00' | 3840997363
<class 'int'> 9 -> <class 'bytes'> b'\t\x00\x00\x00' | 1548521622
---
@
keepeye bytearray 出来的结果是 class 'bytearray',所以我改了下。
j = bytes([i])
<class 'int'> 0 -> <class 'bytes'> b'\x00' | 3523407757
<class 'int'> 1 -> <class 'bytes'> b'\x01' | 2768625435
<class 'int'> 2 -> <class 'bytes'> b'\x02' | 1007455905
<class 'int'> 3 -> <class 'bytes'> b'\x03' | 1259060791
<class 'int'> 4 -> <class 'bytes'> b'\x04' | 3580832660
<class 'int'> 5 -> <class 'bytes'> b'\x05' | 2724731650
<class 'int'> 6 -> <class 'bytes'> b'\x06' | 996231864
<class 'int'> 7 -> <class 'bytes'> b'\x07' | 1281784366
<class 'int'> 8 -> <class 'bytes'> b'\x08' | 3705235391
<class 'int'> 9 -> <class 'bytes'> b'\t' | 2883475241
---
@
wuwukai007 pandas 的方法,结果是正确的,就是圈子兜得也略有点大。
j = pd.Series([i]).astype('bytes').tolist()[0]
<class 'int'> 0 -> <class 'bytes'> b'0' | 4108050209
<class 'int'> 1 -> <class 'bytes'> b'1' | 2212294583
<class 'int'> 2 -> <class 'bytes'> b'2' | 450215437
<class 'int'> 3 -> <class 'bytes'> b'3' | 1842515611
<class 'int'> 4 -> <class 'bytes'> b'4' | 4088798008
<class 'int'> 5 -> <class 'bytes'> b'5' | 2226203566
<class 'int'> 6 -> <class 'bytes'> b'6' | 498629140
<class 'int'> 7 -> <class 'bytes'> b'7' | 1790921346
<class 'int'> 8 -> <class 'bytes'> b'8' | 4194326291
<class 'int'> 9 -> <class 'bytes'> b'9' | 2366072709
---
@
ysc3839 to_bytes 我一开始试了,有个长度参数的问题,没想好怎么处理。另外它输出的结果好像也是 b'\x00\x01'这类的。