crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.36k stars 1.62k forks source link

Hex Array Literals #14654

Closed stellarpower closed 4 months ago

stellarpower commented 4 months ago

Feature Request

Don't know if there's a neat and terse way to do this already (probably some way), but, when working with binary data or similar, it's be nice to have a percent syntax for creating array literals of UInts without having to type as much:

%x| 42 af e1 | == [ 0x42, 0xaf, 0xe1 ]

Ideally the length would be inferred from the number of characters, and enforced to be the same for all elements as well as enforced to fir exactly in one of the built-in unsigned integer types.I think anything other than unsigned integers would be fair to be out of scope and cause more confusion.

Thanks

Blacksmoke16 commented 4 months ago

Not exactly the same, but you can do like:

"42afe1".hexbytes # => Bytes[66, 175, 225]
HertzDevil commented 4 months ago

2886

Blacksmoke16 commented 4 months ago

Duplicate of #2886

stellarpower commented 4 months ago

Not exactly the same, but you can do like:

"42afe1".hexbytes # => Bytes[66, 175, 225]

Thanks, I could definitely use that for now. The spaces I think would be very useful, having previously worked with HID Report desciptors where you wanna see the boundary between bytes - but I could split the string on whitespace, rejoin and then call hexbytes.