Skip to content

Commit 80faacb

Browse files
committed
🗓 May 2, 2025 8:44:49 PM
✨ int_to_bytes
1 parent 824d553 commit 80faacb

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

TODO

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ New ideas:
2727
☐ ✨ whitespace encoding https://www.dcode.fr/whitespace-language
2828
☐ beaufort
2929
☐ 🔥 update python_requires in setup.py on python version change
30+
☐ update base85 https://github.com/gorakhargosh/mom/blob/master/mom/codec/base85.py#L343
3031

3132
Bug:
3233

chepy/modules/dataformat.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,24 @@ def str_to_hex(self, delimiter: Union[str, bytes] = b"") -> DataFormatT:
750750
self.state = out
751751
return self
752752

753+
@ChepyDecorators.call_stack
754+
def int_to_bytes(self, length: Union[int, None]=None, byteorder: str = 'big'):
755+
"""Int to bytes conversion
756+
757+
Args:
758+
length (int, optional): Length of bytes. If not specified, byteorder is ignored and long_to_bytes is used.
759+
byteorder (str, optional): Endianness. Defaults to 'big'.
760+
761+
Returns:
762+
Chepy: The Chepy object.
763+
"""
764+
data = self._convert_to_int()
765+
if not length:
766+
self.state = crypto_number.long_to_bytes(self.state)
767+
else:
768+
self.state = data.to_bytes(int(length), byteorder=byteorder)
769+
return self
770+
753771
@ChepyDecorators.call_stack
754772
def int_to_hex(self, unsigned_int: bool = True, bit_size: int = 64) -> DataFormatT:
755773
"""Converts an integer into its hex equivalent

chepy/modules/dataformat.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class DataFormat(ChepyCore):
3737
def hex_to_bytes(self: DataFormatT) -> DataFormatT: ...
3838
def hex_to_str(self: DataFormatT, ignore: bool=...) -> DataFormatT: ...
3939
def str_to_hex(self: DataFormatT, delimiter: Union[str, bytes]=b'') -> DataFormatT: ...
40+
def int_to_bytes(self: DataFormatT, length: Union[int, None], byteorder: Literal['big,little']='big') -> DataFormatT: ...
4041
def int_to_hex(self: DataFormatT, unsigned_int: bool=True, bit_size: int=64) -> DataFormatT: ...
4142
def int_to_str(self: DataFormatT) -> DataFormatT: ...
4243
def binary_to_hex(self: DataFormatT) -> DataFormatT: ...

tests/test_dataformat.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ def test_hex_to_bytes():
202202
assert Chepy("ab00").hex_to_bytes().o == b"\xab\x00"
203203

204204

205+
def test_int_to_bytes():
206+
data = [17235, 17223]
207+
assert Chepy(data[0]).int_to_bytes().o == b"CS"
208+
assert Chepy(data[0]).int_to_bytes(2).o == b"CS"
209+
assert Chepy(data[0]).int_to_bytes(2, "little").o == b"SC"
210+
assert Chepy(data[0]).int_to_bytes(4, "big").o == b"\x00\x00CS"
211+
212+
205213
def test_int_to_hex():
206214
assert Chepy(101).int_to_hex().o == b"65"
207215
assert Chepy(-0x2152411021524111).int_to_hex().o == b"deadbeefdeadbeef"

0 commit comments

Comments
 (0)