-
byte order and floats, what am I doing wrong? I have my own python code, below, that successfully returns the mnemonic_ba = bytearray([ byte_arr[offset+36+1], byte_arr[offset+36+0], byte_arr[offset+36+3], byte_arr[offset+36+2] ])
MNEMONIC = (struct.unpack('<f', mnemonic_ba))[0] However, when I use ccsdspy, with the below definition for PacketField(name='MNEMONIC', data_type='float', bit_length=32, byte_order='little'), the values that are returned are wrong and are either extremely large or extremely small float values. However, all |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 19 replies
-
Maybe I'm missing something, but I haven't encountered 3412 before. The little/big endian orders are 4321 and 1234... is there a name for this 3412? Does it happen a lot in your use cases? What you could do in CCSDSPy is parse two uint fields, one for bytes 34 and one for bytes 12, and then write a custom converter to combine them. Something like this: from ccsdspy import FixedLength, converters
class CustomConverter(converters.Converter):
def __init__(self):
pass
def convert(self, first, second):
# combine them here
pkt = FixedLength([
PacketField(name="first", data_type="uint", bit_length=16),
PacketField(name="second", data_type="uint", bit_length=16),
])
pkt.add_converted_field(
("first", "second"),
"MyField_Converted",
CustomConverter()
)
result = pkt.load("MyCCSDS.bin")
print(result["MyField_Converted"]) I haven't tested this code to implement def convert(self, first, second):
combined = np.array([first, second], dtype=np.uint16).T.flatten()
combined.dtype = np.float32
return combined |
Beta Was this translation helpful? Give feedback.
-
3412 is very common where I work. It's the dominant byte order. I think
permitting the option of providing numeric representation of byte order
would be very useful.
…On Fri, Feb 9, 2024, 22:45 Daniel da Silva ***@***.***> wrote:
Maybe I'm missing something, but I haven't encountered 3412 before. The
little/big endian orders are 4321 and 1234... is there a name for this
3412? Does it happen a lot in your use cases?
What you could do in CCSDSPy is parse two uint fields, one for bytes 34
and one for bytes 12, and then write a custom converter
<https://docs.ccsdspy.org/en/latest/user-guide/converters.html> to
combine them. Something like this:
from ccsdspy import FixedLength, converters
class CustomConverter(converters.Converter):
def __init__(self):
pass
def convert(self, first, second):
# combine them here
pkt = FixedLength([
PacketField(name="first", data_type="uint", bit_length=16),
PacketField(name="second", data_type="uint", bit_length=16),
])pkt.add_converted_field(
("first", "second"),
"MyField_Converted",
CustomConverter()
)
result = pkt.load("MyCCSDS.bin")
print(result["MyField_Converted"])
I haven't tested this code to implement # combine them here, but I think
you could do something like this:
def convert(self, first, second):
combined = np.array([first, second], dtype=np.uint16).T.flatten()
combined.dtype = np.float32
return combined
—
Reply to this email directly, view it on GitHub
<#110 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AADJ6H23OMQGKDALUM62FVLYS3UMZAVCNFSM6AAAAABDCBX7A2VHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4DIMRVGE2DO>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
Great! I changed two lines (basically added
byte_order.reverse()
to the library and test data)... let me know if it is better now.