| imho.ws |
![]() |
|
|
|
# 1 |
|
Junior Member
Регистрация: 30.03.2003
Адрес: СПб
Сообщения: 162
![]() ![]() ![]() |
c#: Чтение строки UTF16 из файла
Есть бинарный файл (база iPod) открытый через BinaryReader. Программа находит Position в файле с которого надо прочитать строку UTF16 (без 0x00 на конце) в string. Длина строки в байтах известна.
Как прочитать эту строку в string? Пробую делать через ReadChars, но читает один байт в один char (в MSDN пишут, что там char двухбайтовый).
__________________
640Kbytes should be enough for everything! (c) Bill Gates, 1981. Все "спасибо" в репутацию
|
|
|
|
|
# 2 |
|
Junior Member
Регистрация: 30.03.2003
Адрес: СПб
Сообщения: 162
![]() ![]() ![]() |
Короче сам разобрался
![]() Код:
private System.Char[] tmpChars; private byte[] tmpBytes; ...... UnicodeEncoding Unicode = new UnicodeEncoding(); ..... BinaryReader binReader = new BinaryReader(File.Open(fileName, FileMode.Open)); ..... tmpChars = new System.Char[stringLength/2]; tmpBytes = new byte[stringLength]; tmpBytes = binReader.ReadBytes(stringLength); tmpChars = Unicode.GetChars(tmpBytes); tmpString = new String(tmpChars);
__________________
640Kbytes should be enough for everything! (c) Bill Gates, 1981. Все "спасибо" в репутацию
|
|
|
|
|
# 4 |
|
Junior Member
Регистрация: 30.03.2003
Адрес: СПб
Сообщения: 162
![]() ![]() ![]() |
Когда я првязываю StreamReader к потоку (BaseStream) BinaryReader, то после вызова Read указатель в потоке "улетает" совсем не туда, куда я ожидаю - при первом чтении на много символов вперёд, при втором - вообще никуда не двигается.
Код:
BinaryReader binReader = new BinaryReader(File.Open(fileName, FileMode.Open)); StreamReader strReader = new StreamReader(binReader.BaseStream,System.Text.Encoding.Unicode); Код:
tmpChars = new System.Char[stringLength/2]; strReader.Read(tmpChars,0,stringLength/2); tmpString = new String(tmpChars);
__________________
640Kbytes should be enough for everything! (c) Bill Gates, 1981. Все "спасибо" в репутацию
|
|
|
|
|
# 5 | |
|
МОД-Оператор ЭВМ
Регистрация: 18.04.2002
Адрес: Питер
Сообщения: 4 343
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Цитата:
Код:
using System;
using System.IO;
using System.Text;
public class FileConverter
{
const int BufferSize = 8096;
public static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine
("Usage: FileConverter <input file> <output file>");
return;
}
// Open a TextReader for the appropriate file
using (TextReader input = new StreamReader
(new FileStream (args[0], FileMode.Open),
Encoding.UTF8))
{
// Open a TextWriter for the appropriate file
using (TextWriter output = new StreamWriter
(new FileStream (args[1], FileMode.Create),
Encoding.Unicode))
{
// Create the buffer
char[] buffer = new char[BufferSize];
int len;
// Repeatedly copy data until we've finished
while ( (len = input.Read (buffer, 0, BufferSize)) > 0)
{
output.Write (buffer, 0, len);
}
}
}
}
}
|
|
|
|