C# Bit Bashing - The BitConverter |
Written by Mike James | ||||||
Tuesday, 20 May 2014 | ||||||
Page 2 of 2
BitConverterOne of the most common reasons for having to resort to bit manipulation is that you have a raw stream of bytes and need to turn them into structured data. As the logical operations don’t work for non-integral types this is something of a problem if you want to convert bytes into floats, say. It is even not particularly easy to convert bytes into integral types. That is the problem that you most often have to solve is that you have a stream of bytes and groups of bytes represent either floating point or integer values and you need to convert the stream into those types. This is where the BitConverter class becomes important. It is intended to help you serialise an object to bytes and back again but this doesn’t stop us using it for other purposes. The BitConverter has a GetBytes method that will convert any of the standard data type into a byte array and a specific ToType method that converts the same byte array back into the appropriate data type. So for example
returns an eight-byte array containing the bits that were stored in the eight-byte double. To reconstruct the double we use:
There are also two alternative forms of the ToType method that let you specify the location in the array of the data – very useful when processing raw byte streams. The BitConverter class allows you to both generate and reconstruct data from byte streams but it also gives you the opportunity to perform logical operations on a range of data types as a byte array and then restore the result. It is important to realise that BitConverter is “little endian” which is something that non-Intel programmers don’t take for granted. For example, what value is stored in x after this:
If you think the answer is 1 then you are working in big endian format, i.e. the high order byte comes first, but the actual answer on Intel-based machines is 256 because they are little endian and take the first byte as the low order byte. Usually this isn’t a problem once you know about it, but it can result in some messy byte switching if you are trying to work with little endian data. String ProblemsThere is one other problem with using BitConverter and strings. What do you think is stored in s in this case?
You might think that it would be “ABC” but the answer is “41-53-4A”, i.e. a string representation of the hexadecimal values stored in the byte array. In short, BitConverter isn’t much use converting raw bytes into strings. The only solution to this problem seems to be to use one of the “unsafe” string constructors that takes a pointer to a byte array.
To use this method you have to compile with “allow unsafe”. If you now try:
you will see that the string “ABC” has been constructed from the raw byte array. There is a lot more to say about bit manipulation in C#, there’s a bit array type for example, but this will have to wait.
If you have any tips or good ways of doing low-level things in managed C# let us know: email the editor or comment. Related Articles
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
Comments
or email your comment to: comments@i-programmer.info <ASIN:0735668019> <ASIN:0672336901> <ASIN:073568183X> <ASIN:1890774723> <ASIN:1449320414> <ASIN:1118847288> |
||||||
Last Updated ( Tuesday, 20 May 2014 ) |