From f140e26fc6b2fddf2159aaac35dec7b82b9b1e0a Mon Sep 17 00:00:00 2001 From: zh-beep Date: Wed, 12 May 2021 00:14:48 -0500 Subject: [PATCH 1/6] Issue #1 - implement hex_get_byes_function. Returns a hex string, either 0 or the reduced full hex number. --- d8s_math/maths.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/d8s_math/maths.py b/d8s_math/maths.py index e64665d..ab8e1c0 100644 --- a/d8s_math/maths.py +++ b/d8s_math/maths.py @@ -688,3 +688,15 @@ 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 + if ((hex_num_bytes < number_of_bytes) or (hex_num_bytes == number_of_bytes)): + return '0' + + else: + hex_number = hex_number >> (8*number_of_bytes) + final_hex = hex(hex_number) + return final_hex + From 958e34569b7820ba35b34822ba80d8acf23b5ea0 Mon Sep 17 00:00:00 2001 From: Z Date: Wed, 12 May 2021 00:16:58 -0500 Subject: [PATCH 2/6] Update maths.py Added a space to line 693 --- d8s_math/maths.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/d8s_math/maths.py b/d8s_math/maths.py index ab8e1c0..46e19f3 100644 --- a/d8s_math/maths.py +++ b/d8s_math/maths.py @@ -690,7 +690,7 @@ def number_to_engineering_notation(number): return decimal_form.normalize().to_eng_string() def hex_get_bytes(hex_number,number_of_bytes): - length = len(hex(hex_number)) -2 + length = len(hex(hex_number)) - 2 hex_num_bytes = length/2 if ((hex_num_bytes < number_of_bytes) or (hex_num_bytes == number_of_bytes)): return '0' From 5f892531f7b0cbc89522b4d3f4cc876eba4ff175 Mon Sep 17 00:00:00 2001 From: Z Date: Sat, 22 May 2021 00:18:02 -0500 Subject: [PATCH 3/6] Style change in function call Co-authored-by: Floyd Hightower --- d8s_math/maths.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/d8s_math/maths.py b/d8s_math/maths.py index 46e19f3..c3e77c7 100644 --- a/d8s_math/maths.py +++ b/d8s_math/maths.py @@ -689,7 +689,7 @@ 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): +def hex_get_bytes(hex_number, number_of_bytes): length = len(hex(hex_number)) - 2 hex_num_bytes = length/2 if ((hex_num_bytes < number_of_bytes) or (hex_num_bytes == number_of_bytes)): @@ -699,4 +699,3 @@ def hex_get_bytes(hex_number,number_of_bytes): hex_number = hex_number >> (8*number_of_bytes) final_hex = hex(hex_number) return final_hex - From 9897b9a46373c90be5a2fc6e5ddf26a5bc5c1107 Mon Sep 17 00:00:00 2001 From: Z Date: Sat, 22 May 2021 00:19:03 -0500 Subject: [PATCH 4/6] No space between if/else Co-authored-by: Floyd Hightower --- d8s_math/maths.py | 1 - 1 file changed, 1 deletion(-) diff --git a/d8s_math/maths.py b/d8s_math/maths.py index c3e77c7..155b690 100644 --- a/d8s_math/maths.py +++ b/d8s_math/maths.py @@ -694,7 +694,6 @@ def hex_get_bytes(hex_number, number_of_bytes): hex_num_bytes = length/2 if ((hex_num_bytes < number_of_bytes) or (hex_num_bytes == number_of_bytes)): return '0' - else: hex_number = hex_number >> (8*number_of_bytes) final_hex = hex(hex_number) From fe1019c253905fff9cca34d5897c190f3bea1a5a Mon Sep 17 00:00:00 2001 From: zh-beep Date: Sat, 22 May 2021 01:31:53 -0500 Subject: [PATCH 5/6] Flipped the bits representation to provide correct functionality per comments on Pull Request. --- d8s_math/maths.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/d8s_math/maths.py b/d8s_math/maths.py index 7a1bd88..4fddc5d 100644 --- a/d8s_math/maths.py +++ b/d8s_math/maths.py @@ -694,9 +694,12 @@ def number_to_engineering_notation(number): 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 '0' + return hex(hex_number) else: - hex_number = hex_number >> (8*number_of_bytes) + hex_number = hex_number >> math.floor((8*(hex_num_bytes - number_of_bytes))) final_hex = hex(hex_number) return final_hex From 47cc9f07f952cd6f923d260253d34fb41a1a1803 Mon Sep 17 00:00:00 2001 From: Z Date: Sat, 22 May 2021 01:45:10 -0500 Subject: [PATCH 6/6] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 37bfb66..c177f08 100644 --- a/README.md +++ b/README.md @@ -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