MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / encode_long

Function encode_long

tools/python-3.11.9-amd64/Lib/pickle.py:354–382  ·  view source on GitHub ↗

r"""Encode a long to a two's complement little-endian binary string. Note that 0 is a special case, returning an empty string, to save a byte in the LONG1 pickling context. >>> encode_long(0) b'' >>> encode_long(255) b'\xff\x00' >>> encode_long(32767) b'\xff

(x)

Source from the content-addressed store, hash-verified

352 return '__main__'
353
354def encode_long(x):
355 r"""Encode a long to a two's complement little-endian binary string.
356 Note that 0 is a special case, returning an empty string, to save a
357 byte in the LONG1 pickling context.
358
359 >>> encode_long(0)
360 b''
361 >>> encode_long(255)
362 b'\xff\x00'
363 >>> encode_long(32767)
364 b'\xff\x7f'
365 >>> encode_long(-256)
366 b'\x00\xff'
367 >>> encode_long(-32768)
368 b'\x00\x80'
369 >>> encode_long(-128)
370 b'\x80'
371 >>> encode_long(127)
372 b'\x7f'
373 >>>
374 """
375 if x == 0:
376 return b''
377 nbytes = (x.bit_length() >> 3) + 1
378 result = x.to_bytes(nbytes, byteorder='little', signed=True)
379 if x < 0 and nbytes > 1:
380 if result[-1] == 0xff and (result[-2] & 0x80) != 0:
381 result = result[:-1]
382 return result
383
384def decode_long(data):
385 r"""Decode a long from a two&#x27;s complement little-endian binary string.

Callers 1

save_longMethod · 0.85

Calls 1

to_bytesMethod · 0.80

Tested by

no test coverage detected