【发布时间】:2010-12-14 01:08:05
【问题描述】:
.NET 框架中是否有内置的Gray code 数据类型?或者灰色和二进制之间的转换实用程序?我可以自己做,但如果轮子已经被发明出来的话......
【问题讨论】:
-
感谢维基百科链接。从未听说过灰色代码,这使阅读变得有趣。
标签: c# .net error-checking gray-code
.NET 框架中是否有内置的Gray code 数据类型?或者灰色和二进制之间的转换实用程序?我可以自己做,但如果轮子已经被发明出来的话......
【问题讨论】:
标签: c# .net error-checking gray-code
使用this trick。
/*
The purpose of this function is to convert an unsigned
binary number to reflected binary Gray code.
*/
unsigned short binaryToGray(unsigned short num)
{
return (num>>1) ^ num;
}
一个棘手的技巧:对于最多 2^n 位,您可以通过以下方式将 Gray 转换为二进制 执行 (2^n) - 1 次二进制到灰度转换。您所需要的只是 上面的函数和一个'for'循环。
/*
The purpose of this function is to convert a reflected binary
Gray code number to a binary number.
*/
unsigned short grayToBinary(unsigned short num)
{
unsigned short temp = num ^ (num>>8);
temp ^= (temp>>4);
temp ^= (temp>>2);
temp ^= (temp>>1);
return temp;
}
【讨论】:
这是一个 C# 实现,假设您只想对非负 32 位整数执行此操作:
static uint BinaryToGray(uint num)
{
return (num>>1) ^ num;
}
您可能还想阅读this blog post,它提供了双向转换的方法,尽管作者选择将代码表示为int 的数组,每个位置包含一个或零。我个人认为BitArray 可能是更好的选择。
【讨论】:
也许这个方法集合很有用
尽情享受吧。
public static class GrayCode
{
public static byte BinaryToByte(BitArray binary)
{
if (binary.Length > 8)
throw new ArgumentException("bitarray too long for byte");
var array = new byte[1];
binary.CopyTo(array, 0);
return array[0];
}
public static int BinaryToInt(BitArray binary)
{
if (binary.Length > 32)
throw new ArgumentException("bitarray too long for int");
var array = new int[1];
binary.CopyTo(array, 0);
return array[0];
}
public static BitArray BinaryToGray(BitArray binary)
{
var len = binary.Length;
var gray = new BitArray(len);
gray[len - 1] = binary[len - 1]; // copy high-order bit
for (var i = len - 2; i >= 0; --i)
{
// remaining bits
gray[i] = binary[i] ^ binary[i + 1];
}
return gray;
}
public static BitArray GrayToBinary(BitArray gray)
{
var len = gray.Length;
var binary = new BitArray(len);
binary[len - 1] = gray[len - 1]; // copy high-order bit
for (var i = len - 2; i >= 0; --i)
{
// remaining bits
binary[i] = !gray[i] ^ !binary[i + 1];
}
return binary;
}
public static BitArray ByteToGray(byte value)
{
var bits = new BitArray(new[] { value });
return BinaryToGray(bits);
}
public static BitArray IntToGray(int value)
{
var bits = new BitArray(new[] { value });
return BinaryToGray(bits);
}
public static byte GrayToByte(BitArray gray)
{
var binary = GrayToBinary(gray);
return BinaryToByte(binary);
}
public static int GrayToInt(BitArray gray)
{
var binary = GrayToBinary(gray);
return BinaryToInt(binary);
}
/// <summary>
/// Returns the bits as string of '0' and '1' (LSB is right)
/// </summary>
/// <param name="bits"></param>
/// <returns></returns>
public static string AsString(this BitArray bits)
{
var sb = new StringBuilder(bits.Length);
for (var i = bits.Length - 1; i >= 0; i--)
{
sb.Append(bits[i] ? "1" : "0");
}
return sb.ToString();
}
public static IEnumerable<bool> Bits(this BitArray bits)
{
return bits.Cast<bool>();
}
public static bool[] AsBoolArr(this BitArray bits, int count)
{
return bits.Bits().Take(count).ToArray();
}
}
【讨论】:
IntToBinary 函数。如果每个功能都独立于其他功能,那就太好了。
var bits = new BitArray(new[] { value });
.NET 中没有任何内置格雷码。
【讨论】:
Graphical Explanation关于格雷码转换 - 这可以帮助一点
【讨论】: