Skip to content

Commit

Permalink
Merge pull request #6 from zh-beep/main
Browse files Browse the repository at this point in the history
Implemented hex_get_bytes function
  • Loading branch information
fhightower authored May 25, 2021
2 parents 2fcb673 + 47cc9f0 commit 7bf6adb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ Once imported, you can use any of the functions listed below.
def number_to_engineering_notation(number):
"""Convert the given number to engineering notation."""
```
- ```python
def hex_get_bytes(hex_number, number_of_bytes):
"""Reduce a hex number number to a specific number of bytes. For example, (0x123456,2) returns '0x1234'."""
```

## Development

Expand Down
13 changes: 13 additions & 0 deletions d8s_math/maths.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,3 +690,16 @@ def number_to_engineering_notation(number):

decimal_form = decimal.Decimal(number)
return decimal_form.normalize().to_eng_string()

def hex_get_bytes(hex_number, number_of_bytes):
length = len(hex(hex_number)) - 2
hex_num_bytes = length/2
# print(type(hex_num_bytes))
# print(type(number_of_bytes))
# print(hex_num_bytes - number_of_bytes)
if ((hex_num_bytes < number_of_bytes) or (hex_num_bytes == number_of_bytes)):
return hex(hex_number)
else:
hex_number = hex_number >> math.floor((8*(hex_num_bytes - number_of_bytes)))
final_hex = hex(hex_number)
return final_hex

0 comments on commit 7bf6adb

Please sign in to comment.