2011-01-07 86 views
3

我怎麼能寫在代碼的二進制數,而無需使用我可以用二進制而不是十六進制嵌入數字嗎?

Convert.ToInt16("00001000", 2); 

我想這是非常快的,我不希望從字符串讀取數。 有沒有更快的方法?

+8

你的意思是你想要`int i = 00001000b;`或`0b00001000`嗎? – 2011-01-07 13:47:31

+0

在進行轉換之前,您有什麼格式的二進制數字? – 2011-01-07 13:48:08

回答

4

從C#語言規範:

2.4.4.2整數常量

整數數用於編寫類型 值INT,UINT,長,和 ULONG。整數文字有兩個 可能的形式:十進制和 十六進制。

你可以做你的方式:

Convert.ToInt16("00001000", 2); 

或者使用0x前綴代表例如十六進制格式的數字,如:

0x8 

我不知道有任何另一種方式。

1

通常,爲了避免任何愚蠢的錯誤,當我定義一個「flags」枚舉時,我將使用位移運算符來指定值,並讓編譯器找出它。例如:

[Flags] 
enum FlagsEnum 
{ 
    Zero = 0, 
    One = 1 << 0, 
    Two = 1 << 1, 
    Four = 1 << 2, 
    Eight = 1 << 3, 
    Sixteen = 1 << 4, 
    ThirtyTwo = 1 << 5, 
    SixtyFour = 1 << 6, 
    OneHundredTwentyEight = 1 << 7, 
    // etc. 
} 

我覺得這種風格更容易維護,當我不可避免地重新排序並添加/從我的枚舉刪除值。

所以在你的情況下,你可以指定你的二進制值「00001000」爲(1 << 3)。如果你有更復雜的東西,比如「00101000」,那麼這樣做不會太好。你可能做一些像(1 << 5) | (1 << 3),但對於那種情況下,我只是指定十六進制值。

0

我有一類特殊的這種

public static class B 
{ 
    public static readonly V _0000 = 0x0; 
    public static readonly V _0001 = 0x1; 
    public static readonly V _0010 = 0x2; 
    public static readonly V _0011 = 0x3; 
    public static readonly V _0100 = 0x4; 
    public static readonly V _0101 = 0x5; 
    public static readonly V _0110 = 0x6; 
    public static readonly V _0111 = 0x7; 

    public static readonly V _1000 = 0x8; 
    public static readonly V _1001 = 0x9; 
    public static readonly V _1010 = 0xA; 
    public static readonly V _1011 = 0xB; 
    public static readonly V _1100 = 0xC; 
    public static readonly V _1101 = 0xD; 
    public static readonly V _1110 = 0xE; 
    public static readonly V _1111 = 0xF; 

    public struct V 
    { 
     ulong Value; 

     public V(ulong value) 
     { 
      this.Value = value; 
     } 

     private V Shift(ulong value) 
     { 
      return new V((this.Value << 4) + value); 
     } 

     public V _0000 { get { return this.Shift(0x0); } } 
     public V _0001 { get { return this.Shift(0x1); } } 
     public V _0010 { get { return this.Shift(0x2); } } 
     public V _0011 { get { return this.Shift(0x3); } } 
     public V _0100 { get { return this.Shift(0x4); } } 
     public V _0101 { get { return this.Shift(0x5); } } 
     public V _0110 { get { return this.Shift(0x6); } } 
     public V _0111 { get { return this.Shift(0x7); } } 

     public V _1000 { get { return this.Shift(0x8); } } 
     public V _1001 { get { return this.Shift(0x9); } } 
     public V _1010 { get { return this.Shift(0xA); } } 
     public V _1011 { get { return this.Shift(0xB); } } 
     public V _1100 { get { return this.Shift(0xC); } } 
     public V _1101 { get { return this.Shift(0xD); } } 
     public V _1110 { get { return this.Shift(0xE); } } 
     public V _1111 { get { return this.Shift(0xF); } } 

     static public implicit operator V(ulong value) 
     { 
      return new V(value); 
     } 

     static public implicit operator ulong(V this_) 
     { 
      return this_.Value; 
     } 

     static public implicit operator uint(V this_) 
     { 
      return (uint)this_.Value; 
     } 

     static public implicit operator ushort(V this_) 
     { 
      return (ushort)this_.Value; 
     } 

     static public implicit operator byte(V this_) 
     { 
      return (byte)this_.Value; 
     } 
    } 
} 

例子:在實現CityLizard Framework (CityLizard.Binary.dll)

byte x = B._0010._0011; 
ushort y = B._1001._0011._1111._0000; 
uint z = B._1001._0011._1111._1000._0011; 
ulong d = B._1001._0011._1111._1000._0011._0001; 

相關問題