使用BinaryReader读取“little-endian UTF-16编码中的字符串”
我遵循以下文件格式的规范: https : //github.com/rouault/dump_gdbtable/wiki/FGDB-Spec
utf16: string in little-endian UTF-16 encoding
我怎么读这个? 我尝试了BinaryReader.ReadString()但它返回的内容如下:
"\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字符的数量来读取字段的名称?
BinaryReader
的ReadString()
方法不提供重载,您可以在其中指定字符串长度(而不是它假定编码的前缀长度,它与您链接的规范的格式不匹配)。
因此,您不能直接使用ReadString()
,但您可以
- 使用
ReadByte()
来获取字符串(字符)长度, - 乘以2,
- 使用
ReadBytes(count)
, - 使用
Encoding.Unicode.GetString(bytes)
。
它应该是 :
BinaryReader br = new BinaryReader(File.Open("C:\\florida.gdb\\a00000002.gdbtable", FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete), Encoding.Unicode);
Encoding
是System.Text.Encoding
。
由于各种历史原因,Microsoft / Windows将UTF-16(具体而言,小端变体)称为“Unicode”而不是UTF-16。