Binary text conversion help class, binary text Conversion

Source: Internet
Author: User

Binary text conversion help class, binary text Conversion

http://www.codeproject.com/Tips/1080310/Csharp-Binary-Literal-Helper-Class

C # direct assignment of binary data is not supported currently

      int bits=0b00010101;

If values can be assigned directly, it will be very convenient in bitwise operations, so you can use this class to directly assign values to variables in binary text.

 1    public static class BinaryLiteral 2     { 3         public static byte ToByte(string str) 4         { 5             return (byte)ToInt64(str, sizeof(byte)); 6         } 7  8         public static short ToInt16(string str) 9         {10             return (short)ToInt64(str, sizeof(short));11         }12 13         public static int ToInt32(string str)14         {15             return (int)ToInt64(str, sizeof(int));16         }17 18         public static long ToInt64(string str)19         {20             return ToInt64(str, sizeof(long));21         }22 23         private static long ToInt64(string str, int sizeInBytes)24         {25             int sizeInBits = sizeInBytes * 8;26             int bitIndex = 0;27             long result = 0;28 29             for (int i = str.Length - 1; i >= 0; i--)30             {31                 if (str[i] != ' ')32                 {33                     if (bitIndex == sizeInBits)34                     {35                         throw new OverflowException("binary literal too long: " + str);36                     }37 38                     if (str[i] == '1')39                     {40                         result |= 1L << bitIndex;41                     }42                     else if (str[i] != '0')43                     {44                         throw new InvalidCastException("invalid character in binary literal: " + str[i]);45                     }46 47                     bitIndex++;48                 }49             }50 51             return result;52         }53     }

You can use them like this

1  byte myByteBitMask = BinaryLiteral.ToByte("                        0110 1100");2  short myInt16BitMask = BinaryLiteral.ToInt16("          1000 0000 0110 1100");3  int myInt32BitMask = BinaryLiteral.ToInt32("   0101 1111 1000 0000 0110 1100");4  long myInt64BitMask = BinaryLiteral.ToInt64("  0101 1111 1000 0000 0110 1100");

You can also use the extension method to make the assignment easier.

 public static int ToInt32(this string str) {     return (int)ToInt64(str, sizeof(int)); }
int myInt32BitMask2 = "   0101 1111 1000 0000 0110 1100".ToInt32();

You can use underscores (_) or other symbols to represent the delimiters in binary data. You only need to change the condition of the space character in the ToInt64 Method to the corresponding one.

This class has a limitation that it cannot be used for const constant initialization, but you can assign values to the static readonly read-only variable.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.