Skip to content
Snippets Groups Projects
Commit fdae3172 authored by Johannes Kung's avatar Johannes Kung
Browse files

Implemented more formats

parent fa064bdf
No related branches found
No related tags found
No related merge requests found
Pipeline #134598 failed
...@@ -82,17 +82,17 @@ def format_to_str(format_info: FormatInfo, value: Any) -> str: ...@@ -82,17 +82,17 @@ def format_to_str(format_info: FormatInfo, value: Any) -> str:
""" """
match format_info.selected_format: match format_info.selected_format:
case Format.Int: case Format.Int:
pass return str(value)
case Format.Str: case Format.Str:
pass return str(value)
case Format.Bin: case Format.Bin:
return f"{value:0{format_info.kwargs['bit_length']}b}" return f"{value:0{format_info.kwargs['bit_length']}b}"
case Format.Hex: case Format.Hex:
return f"{value:0{math.ceil(format_info.kwargs['bit_length']/4)}x}" return f"{value:0{math.ceil(format_info.kwargs['bit_length']/4)}x}"
case Format.Decimal: case Format.Decimal:
pass return str(value)
case Format.DecimalSigned: case Format.DecimalSigned:
pass return str(value - 2 ** format_info.kwargs['bit_length'])
def parse_str(format_info: FormatInfo, value: str) -> Any: def parse_str(format_info: FormatInfo, value: str) -> Any:
...@@ -114,5 +114,13 @@ def parse_str(format_info: FormatInfo, value: str) -> Any: ...@@ -114,5 +114,13 @@ def parse_str(format_info: FormatInfo, value: str) -> Any:
match format_info.selected_format: match format_info.selected_format:
case Format.Int: case Format.Int:
return int(value) return int(value)
case Format.Bin:
return int(value, base=2)
case Format.Hex: case Format.Hex:
return int(value, base=16) return int(value, base=16)
case Format.Decimal:
return int(value)
case Format.DecimalSigned:
# Turn negative numbers into the correct unsigned value
# e.g. bit length 3: -1 (0b111 in 2's complement) => 7
return int(value) % 2 ** format_info.kwargs['bit_length']
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment