lunixbochs / struc

Better binary packing for Go
MIT License
564 stars 42 forks source link

Zero padded strings #53

Closed abourget closed 4 years ago

abourget commented 6 years ago

Hi,

I was wondering if it'd be hard to build a zeroed flag or something, that would make this work:

    Precision byte   `struc:"uint8"`
    Name      string `struc:"[7]uint8"`
}

the Name is 7 bytes, but it's zero padded at the end, I'd like to have the string without the zeroes, and pad back with zeroes when writing.. I tried pad.. but it seems to simply ignore the field, right ?

So the data: 0443555200000000 would deserialize to Currency{4, "CUR"} instead of Currency{4, "CUR\x00\x00\x00\x00"}.

lunixbochs commented 6 years ago

42 #31

Does it pack correctly? The easiest workaround for now is to trim nulls yourself using strings.TrimRight.

Another option is to write a custom type.

abourget commented 4 years ago

ok, thanks