如何解决将十六进制转换为 unicode
我正在尝试从 IFC 格式文件中读取一些文本字段,所有这些字段似乎都是十六进制的。 (结果可能是一些俄文文本)。
我可以找到描述如何转换为 Ascii 的不同帖子,并且还找到了有关如何convert from hexadecimal to string for unicode 的答案,这是给出的代码:
public static string FromHexString(string hexString)
{
var bytes = new byte[hexString.Length / 2];
for (var i = 0; i < bytes.Length; i++)
{
bytes[i] = Convert.ToByte(hexString.Substring(i * 2,2),16);
}
return Encoding.Unicode.GetString(bytes); // returns: "Hello world" for "48656C6C6F20776F726C64"
}
原始字符串看起来像 \X2\0420043504310440043E\X0\
。我不知道 \X2\
和 \X0\
是什么意思,但我猜(错?)它特定于 IFC 格式来定义编码???
解决方法
编码使用的是 Big Endian Unicode:
Dim Rows As Integer
Dim Kolumns As Integer
Dim Start As Range
Set Start = Sheets("sheet1").Range("B2")
Start.Select
Rows = 10
Kolumns = 5
For i = 1 To Rows
For j = 1 To Kolumns
ListBox1.AddItem
ListBox1.List(i - 1,j - 1) = ActiveCell.Offset(i - 1,j - 1).Value
Next j
Next i
使用您的字符串和来自 http://www.steptools.com/stds/step/IS_final_p21e3.html 的测试用例进行测试
请注意,整个 IFC 格式非常复杂。编写一个支持各种 return Encoding.BigEndianUnicode.GetString(bytes);
、\S\(something)
、\P(something)\
、\X2\(hex)
、\X4\(hex)
(加上结束 \X\(hex)
)。在关于 \X0\
示例的文档中甚至存在一个问题(用 7 个十六进制数字而不是 8 个十六进制数字给出),并且似乎整个文件应该在转义序列之外使用 UTF-8 编码。
Aaaa 完成:
一些测试:
\X4\
和解码器:
// With .NET Core/.NET 5.0 you'll need the nuget
// https://www.nuget.org/packages/System.Text.Encoding.CodePages/
// And this line
//Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// Nothing is needed with .NET Framework
string strExampleUnquoted = ItfStringDecoder.DecodeItfString(@"\X2\0420043504310440043E\X0\");
string str1Unquoted = ItfStringDecoder.DecodeItfString(@"CAT");
string str2Unquoted = ItfStringDecoder.DecodeItfString(@"Don''t");
string str3Unquoted = ItfStringDecoder.DecodeItfString(@"''");
string str4Unquoted = ItfStringDecoder.DecodeItfString(@"");
string str5Unquoted = ItfStringDecoder.DecodeItfString(@"\S\Drger");
string str6Unquoted = ItfStringDecoder.DecodeItfString(@"h\S\ttel");
string str7Unquoted = ItfStringDecoder.DecodeItfString(@"\PE\\S\*\S\U\S\b");
string str8Unquoted = ItfStringDecoder.DecodeItfString(@"\X2\03C0\X0\");
string str9Unquoted = ItfStringDecoder.DecodeItfString(@"\X2\03B103B203B3\X0\");
string str10Unquoted = ItfStringDecoder.DecodeItfString(@"\X4\0001F600\X0\");
string str11Unquoted = ItfStringDecoder.DecodeItfString(@"\X4\0001F6000001F638\X0\");
string str12Unquoted = ItfStringDecoder.DecodeItfString(@"see \X\A7 4.1");
string str13Unquoted = ItfStringDecoder.DecodeItfString(@"line one\X\0Aline two");
string str1Quoted = ItfStringDecoder.DecodeItfString(@"'CAT'",true);
string str2Quoted = ItfStringDecoder.DecodeItfString(@"'Don''t'",true);
string str3Quoted = ItfStringDecoder.DecodeItfString(@"''''",true);
string str4Quoted = ItfStringDecoder.DecodeItfString(@"''",true);
string str5Quoted = ItfStringDecoder.DecodeItfString(@"'\S\Drger'",true);
string str6Quoted = ItfStringDecoder.DecodeItfString(@"'h\S\ttel'",true);
string str7Quoted = ItfStringDecoder.DecodeItfString(@"'\PE\\S\*\S\U\S\b'",true);
string str8Quoted = ItfStringDecoder.DecodeItfString(@"'\X2\03C0\X0\'",true);
string str9Quoted = ItfStringDecoder.DecodeItfString(@"'\X2\03B103B203B3\X0\'",true);
string str10Quoted = ItfStringDecoder.DecodeItfString(@"'\X4\0001F600\X0\'",true);
string str11Quoted = ItfStringDecoder.DecodeItfString(@"'\X4\0001F6000001F638\X0\'",true);
string str12Quoted = ItfStringDecoder.DecodeItfString(@"'see \X\A7 4.1'",true);
string str13Quoted = ItfStringDecoder.DecodeItfString(@"'line one\X\0Aline two'",true);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。