我正在从另一个系统检索使用代码页 437 编码的 ASCII 字符串,我需要将其转换为 Unicode,以便它们可以与其他 Unicode 字符串混合。
这就是我正在使用的:
var asciiString = "\u0094"; // 94 corresponds represents 'ö' in code page 437.
var asciiEncoding = Encoding.GetEncoding(437);
var unicodeEncoding = Encoding.Unicode;
// This is what I attempted to do but it seems not to be able to support the eight bit. Characters using the eight bit are replaced with '?' (0x3F)
var asciiBytes = asciiEncoding.GetBytes(asciiString);
// This work-around does the job, but there must be built in functionality to do this?
//var asciiBytes = asciiString.Select(c => (byte)c).ToArray();
// This piece of code happliy converts the character correctly to unicode { 0x94 } => { 0xF6, 0x0 } .
var unicodeBytes = Encoding.Convert(asciiEncoding, unicodeEncoding, asciiBytes);
var unicodeString = unicodeEncoding.GetString(unicodeBytes); // I want this to be 'ö'.
我正在苦苦挣扎的是,我在 .NET 框架中找不到合适的方法来将字符代码高于 127 的字符串转换为字节数组。这看起来很奇怪,因为那里支持将具有 127 以上字符的字节数组转换为 Unicode 字符串。
所以我的问题是,是否有任何内置方法可以正确进行此转换,或者我的解决方法是正确的方法?