A more readable way to define hex literals #8371
Unanswered
OriginalGriff
asked this question in
Language Ideas
Replies: 2 comments 4 replies
-
You can get most of the way there today: static byte[] HexToBytes(string s) { ... }
byte[] pol1 = HexToBytes("01'00'00'02'FF'FF'FF'FF'03"); |
Beta Was this translation helpful? Give feedback.
3 replies
-
I have built a source generator to do this. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I saw this on CodeProject: https://www.codeproject.com/Messages/6014760/Well-gee-golly-C-done-gone-and-got-fancy-if-you-us And assume that it was the equivelant of this:
byte[] data = 0xFE'DC'BA'98;
vs the original
byte[] data = {0xFE, 0xDC, 0xBA, 0x98};
It isn't, it's the equivelant of the numeric literal underscore added at V7:
long i = 1_000_000_000_000;
vs
long i = 1000000000000;
But it got me thinking how much more readable (and thus maintainable) it would be if hex literals / byte collections could be written in that way, particularly when you need to communicate with external devices, or generate template data for binary formatted files:
byte[] poll = 0x01'00'00'02'FF'FF'FF'FF'03;
vs
byte[] poll = {0x01, 0x00, 0x00, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0x03};
for example.
Basically, the quote character extends the base specifier to produce a byte stream in a more compact and readable form.
Beta Was this translation helpful? Give feedback.
All reactions