Converts the given non-negative integer to hex string. Example: Suppose the input is the following: i = 1234 The input is 0x000004d2 in hex, so the little-endian hex string is "d2040000". Arguments: i {[int]} -- [integer] Raises: ValueErro
(i: int)
| 44 | |
| 45 | |
| 46 | def reformat_hex(i: int) -> bytes: |
| 47 | """ |
| 48 | Converts the given non-negative integer to hex string. |
| 49 | |
| 50 | Example: Suppose the input is the following: |
| 51 | i = 1234 |
| 52 | |
| 53 | The input is 0x000004d2 in hex, so the little-endian hex string is |
| 54 | "d2040000". |
| 55 | |
| 56 | Arguments: |
| 57 | i {[int]} -- [integer] |
| 58 | |
| 59 | Raises: |
| 60 | ValueError -- [input is negative] |
| 61 | |
| 62 | Returns: |
| 63 | 8-char little-endian hex string |
| 64 | |
| 65 | >>> reformat_hex(1234) |
| 66 | b'd2040000' |
| 67 | >>> reformat_hex(666) |
| 68 | b'9a020000' |
| 69 | >>> reformat_hex(0) |
| 70 | b'00000000' |
| 71 | >>> reformat_hex(1234567890) |
| 72 | b'd2029649' |
| 73 | >>> reformat_hex(1234567890987654321) |
| 74 | b'b11c6cb1' |
| 75 | >>> reformat_hex(-1) |
| 76 | Traceback (most recent call last): |
| 77 | ... |
| 78 | ValueError: Input must be non-negative |
| 79 | """ |
| 80 | if i < 0: |
| 81 | raise ValueError("Input must be non-negative") |
| 82 | |
| 83 | hex_rep = format(i, "08x")[-8:] |
| 84 | little_endian_hex = b"" |
| 85 | for j in [3, 2, 1, 0]: |
| 86 | little_endian_hex += hex_rep[2 * j : 2 * j + 2].encode("utf-8") |
| 87 | return little_endian_hex |
| 88 | |
| 89 | |
| 90 | def preprocess(message: bytes) -> bytes: |