读取“小端UTF-16编码的字符串".与BinaryReader [英] Reading a "string in little-endian UTF-16 encoding" with BinaryReader

查看:127
本文介绍了读取“小端UTF-16编码的字符串".与BinaryReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循此文件格式的规范: https://github.com/rouault/dump_gdbtable/wiki/FGDB-Spec

I am following this specification of this file format: https://github.com/rouault/dump_gdbtable/wiki/FGDB-Spec

utf16: string in little-endian UTF-16 encoding

我该如何阅读?我尝试了 BinaryReader.ReadString(),但是它返回的内容类似于:

How do I read this? I tried BinaryReader.ReadString() however it returns something along the lines of:

"\0e\0y\0w\0o\0r\0d\0\0 \0\0\0\0\rP\0a\0r\0a\0m\0e\0t\0e\0r\0N\0a\0m\0e\0\0 \0\0\0\0\fC\0o\0n\0f\0i\0g\0S\0t\0r\0"

那肯定是不对的.

根据规范:

ubyte: number of UTF-16 characters (not bytes) of the name of the field
utf16: name of the field
ubyte: number of UTF-16 characters (not bytes) of the alias of the field. Might be 0
utf16: alias of the field (ommitted if previous field is 0)
ubyte: field type ( 0 = int16, 1 = int32, 2 = float32, 3 = float64, 4 = string, 5 = datetime, 6 = objectid, 7 = geometry, 8 = binary, 9=raster, 10/11 = UUID, 12 = XML )

我能以某种方式使用UTF-16字符数读取字段名称吗?

Could I somehow use the number of UTF-16 characters to read the name of the field?

推荐答案

BinaryReader s的ReadString()方法不会提供可在其中指定字符串长度的重载(相反,它假定编码的前缀长度,而不会这样).与您链接的规范的格式匹配).

BinaryReaders ReadString() method doesn't provide an overload where you can specify the string length (instead it assumes an encoded prefixed length, which doesn't match the format of the spec you linked).

因此,您不能直接使用ReadString(),但是可以

Therefore, you cannot use ReadString() directly, but you can

  1. 使用ReadByte()获取字符串(字符)长度
  2. 将其乘以2
  3. 使用ReadBytes(count)
  4. 使用Encoding.Unicode.GetString(bytes).
  1. use ReadByte() to get the string (character) length,
  2. multiply it by 2,
  3. use ReadBytes(count),
  4. use Encoding.Unicode.GetString(bytes).

这篇关于读取“小端UTF-16编码的字符串".与BinaryReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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