净8位编码 [英] .Net 8-bit Encoding

查看:156
本文介绍了净8位编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的串行端口上,数据传输和8位数据接收数据到一些硬件。我想它存储为字符串,以方便比较,preset的数据将作为字符串或十六进制格式的XML文件存储。我发现,使用Encoding.Default这是ANSI编码,然后将8位数据正确,容易可逆转换,只有当。 ASCII编码将只适用于7位数据,和UTF8 UTF7或不工作也很好,因为我使用的一些字符从1-255。 Encoding.Default将是不错,但我读MSDN上的,它是依赖于OS codePAGE设置,这意味着它可能会表现不同的配置不同的codePAGE。我使用的GetBytes()和GetString广泛使用的编码,但是想的作品所有的时间在任何配置故障保护和便携方法。任何想法或更好的建议的呢?

I'm working on serial port, transmitting and receiving data to some hardware at 8bit data. I would like to store it as string to facilitate comparison, and preset data are stored as string or hex format in xml file. I found out that only when using Encoding.Default which is ANSI encoding then the 8bit data is converted properly and easily reversible. ASCII encoding will only works for 7bit data, and UTF8 or UTF7 doesn't works well too, since I'm using some character from 1-255. Encoding.Default would be just fine, but I read on MSDN that it's dependent on OS codepage setting, which means it might behave differently on different codepage configured. I use GetBytes() and GetString extensively using the Encoding, but would like a failsafe and portable method that works all the time at any configuration. Any idea or better suggestion for this?

推荐答案

的Latin-1又名ISO-8859-1又名codePAGE 28591是一个有用的codePAGE对于这种情况,因为它的范围映射值128-255不变。以下是可以互换:

Latin-1 aka ISO-8859-1 aka codepage 28591 is a useful codepage for this scenario, as it maps values in the range 128-255 unchanged. The following are interchangeable:

Encoding.GetEncoding(28591)
Encoding.GetEncoding("Latin1")
Encoding.GetEncoding("iso-8859-1")

下面code说明了一个事实,对于Latin1的,不像Encoding.Default,在0-255范围内的所有字符映射不变的:

The following code illustrates the fact that for Latin1, unlike Encoding.Default, all characters in the range 0-255 are mapped unchanged:

static void Main(string[] args)
{

    Console.WriteLine("Test Default Encoding returned {0}", TestEncoding(Encoding.Default));
    Console.WriteLine("Test Latin1 Encoding returned {0}", TestEncoding(Encoding.GetEncoding("Latin1")));
    Console.ReadLine();
    return;
}

private static bool CompareBytes(char[] chars, byte[] bytes)
{
    bool result = true;
    if (chars.Length != bytes.Length)
    {
    	Console.WriteLine("Length mismatch {0} bytes and {1} chars" + bytes.Length, chars.Length);
    	return false;
    }
    for (int i = 0; i < chars.Length; i++)
    {
    	int charValue = (int)chars[i];
    	if (charValue != (int)bytes[i])
    	{
    		Console.WriteLine("Byte at index {0} value {1:X4} does not match char {2:X4}", i, (int) bytes[i], charValue);
    		result = false;
    	}
    }
    return result;
}
private static bool TestEncoding(Encoding encoding)
{
    byte[] inputBytes = new byte[256];
    for (int i = 0; i < 256; i++)
    {
    	inputBytes[i] = (byte) i;
    }

    char[] outputChars = encoding.GetChars(inputBytes);
    Console.WriteLine("Comparing input bytes and output chars");
    if (!CompareBytes(outputChars, inputBytes)) return false;

    byte[] outputBytes = encoding.GetBytes(outputChars);
    Console.WriteLine("Comparing output bytes and output chars");
    if (!CompareBytes(outputChars, outputBytes)) return false;

    return true;
}

这篇关于净8位编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆