When I have a string like “0xd8 0xff 0xe0” I do
Text.Split(' ').Select(part => byte.Parse(part, System.Globalization.NumberStyles.HexNumber)).ToArray();
But if I got string like “0xd8ffe0” I don’t know what to do ?
also I’m able for recommendations how to write byte array as one string.
You need to scrub your string before you start parsing it. First, remove the leading 0x, then just skip any spaces as you enumerate the string. But using LINQ for this is probably not the best approach. For one, the code won’t be very readable and it’ll be hard to step through if you’re debugging. But also, there are some tricks you can do to make hex/byte conversions very fast. For example, don’t use Byte.Parse, but instead use array indexing to “look up” the corresponding value.
A while back I implemented a HexEncoding class that derives from the Encoding base class much like ASCIIEncoding and UTF8Encoding, etc. Using it is very simple. It’s pretty well optimized too which can be very important depending on the size of your data.
Here’s the complete class, I know it’s kinda big for a post but I’ve stripped out the doc comments.