是否有一种方法可以在c#中编写二进制文字,比如在十六进制前加上0x?0b不行。
如果不是,有什么简单的方法可以做到呢?某种字符串转换?
是否有一种方法可以在c#中编写二进制文字,比如在十六进制前加上0x?0b不行。
如果不是,有什么简单的方法可以做到呢?某种字符串转换?
当前回答
string sTable="static class BinaryTable\r\n{";
string stemp = "";
for (int i = 0; i < 256; i++)
{
stemp = System.Convert.ToString(i, 2);
while(stemp.Length<8) stemp = "0" + stemp;
sTable += "\tconst char nb" + stemp + "=" + i.ToString() + ";\r\n";
}
sTable += "}";
Clipboard.Clear();
Clipboard.SetText ( sTable);
MessageBox.Show(sTable);
使用这个,对于8位二进制,我用它来做一个静态类,它把它放入剪贴板。然后它被粘贴到项目中并添加到Using部分,因此任何与nb001010有关的内容都从表中取出,至少是静态的,但仍然…… 我使用c#进行大量的PIC图形编码,并在high - tech C中大量使用0b101010
——从代码输出的样本——
static class BinaryTable
{ const char nb00000000=0;
const char nb00000001=1;
const char nb00000010=2;
const char nb00000011=3;
const char nb00000100=4;
//etc, etc, etc, etc, etc, etc, etc,
}
:-) 尼尔
其他回答
string sTable="static class BinaryTable\r\n{";
string stemp = "";
for (int i = 0; i < 256; i++)
{
stemp = System.Convert.ToString(i, 2);
while(stemp.Length<8) stemp = "0" + stemp;
sTable += "\tconst char nb" + stemp + "=" + i.ToString() + ";\r\n";
}
sTable += "}";
Clipboard.Clear();
Clipboard.SetText ( sTable);
MessageBox.Show(sTable);
使用这个,对于8位二进制,我用它来做一个静态类,它把它放入剪贴板。然后它被粘贴到项目中并添加到Using部分,因此任何与nb001010有关的内容都从表中取出,至少是静态的,但仍然…… 我使用c#进行大量的PIC图形编码,并在high - tech C中大量使用0b101010
——从代码输出的样本——
static class BinaryTable
{ const char nb00000000=0;
const char nb00000001=1;
const char nb00000010=2;
const char nb00000011=3;
const char nb00000100=4;
//etc, etc, etc, etc, etc, etc, etc,
}
:-) 尼尔
在@StriplingWarrior关于枚举中的位标志的回答中,有一个简单的约定,你可以在十六进制中使用,通过位移位向上计数。使用序列1-2-3 -8,向左移动一列,重复。
[Flags]
enum Scenery
{
Trees = 0x001, // 000000000001
Grass = 0x002, // 000000000010
Flowers = 0x004, // 000000000100
Cactus = 0x008, // 000000001000
Birds = 0x010, // 000000010000
Bushes = 0x020, // 000000100000
Shrubs = 0x040, // 000001000000
Trails = 0x080, // 000010000000
Ferns = 0x100, // 000100000000
Rocks = 0x200, // 001000000000
Animals = 0x400, // 010000000000
Moss = 0x800, // 100000000000
}
从右栏开始向下扫描,注意1-2-4-8 (shift) 1-2-4-8 (shift)…
为了回答最初的问题,我赞同@Sahuagin的建议,使用十六进制字面量。如果您经常使用二进制数,以至于这成为一个问题,那么值得您花点时间来掌握十六进制的诀窍。
如果您需要在源代码中看到二进制数字,我建议像上面那样添加带有二进制文字的注释。
从Visual Studio 2017 (c# 7.0)开始,您可以使用0b000001
虽然字符串解析解决方案是最流行的,但我不喜欢它,因为在某些情况下,解析字符串会极大地影响性能。
当需要一种位域或二进制掩码时,我宁愿这样写
long bitMask = 1011001;
后来
int bit5 = BitField。GetBit(位掩码,5);
Or
bool flag5 = BitField。GetFlag(位掩码,5);”
BitField类在哪里
public static class BitField
{
public static int GetBit(int bitField, int index)
{
return (bitField / (int)Math.Pow(10, index)) % 10;
}
public static bool GetFlag(int bitField, int index)
{
return GetBit(bitField, index) == 1;
}
}
更新
c# 7.0现在有二进制字面值,这非常棒。
[Flags]
enum Days
{
None = 0,
Sunday = 0b0000001,
Monday = 0b0000010, // 2
Tuesday = 0b0000100, // 4
Wednesday = 0b0001000, // 8
Thursday = 0b0010000, // 16
Friday = 0b0100000, // etc.
Saturday = 0b1000000,
Weekend = Saturday | Sunday,
Weekdays = Monday | Tuesday | Wednesday | Thursday | Friday
}
最初的发布
由于主题似乎已经转向在枚举中声明基于位的标志值,因此我认为有必要指出这类事情的一个方便技巧。左移操作符(<<)将允许您将位推到特定的二进制位置。将其与根据同一类中的其他值声明枚举值的能力结合起来,您就拥有了非常易于阅读的位标志枚举声明性语法。
[Flags]
enum Days
{
None = 0,
Sunday = 1,
Monday = 1 << 1, // 2
Tuesday = 1 << 2, // 4
Wednesday = 1 << 3, // 8
Thursday = 1 << 4, // 16
Friday = 1 << 5, // etc.
Saturday = 1 << 6,
Weekend = Saturday | Sunday,
Weekdays = Monday | Tuesday | Wednesday | Thursday | Friday
}