TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Jay S
NA
230
59.7k
BinaryReader/Writer Encoding
Jun 12 2014 5:18 AM
Hi all,
Currently having some issues with encoding when using binarywriter and the way it handles accented characters. I can read in without any issues.
This what I am currently doing:
System.IO.FileStream input = System.IO.File.Open(this.path, System.IO.FileMode.Open);
System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(input, System.Text.Encoding.UTF8);
//
//
//
int nameVol = binaryReader.ReadInt32();
string text2x = string.Empty;
this.names = new System.Collections.Generic.List<Names>();
while (j < nameVol)
{
byte b = binaryReader.ReadByte();
if (b == 0)
{
offset = j;
}
if (b != 0)
{
text2x += (char)b;
}
else
{
int nOffset = (offset - text2x.Length);
names.Add(new Names { name = text2x, offset = nOffset });
text2x = string.Empty;
}
j++;
}
This is fine, it works well and populates my list.
Within my list I have names with accents, again these display correct.
For example it could show Sébastian . Again, all good!
When I come to writing names out I do something like this:
System.IO.FileStream output = System.IO.File.OpenWrite(exportPath);
System.IO.BinaryWriter binaryWriter = new System.IO.BinaryWriter(output);
//
//
//
foreach (Name n in this.names)
{
sNames.Write(binaryWriter);
}
Which does:
public void Write(System.IO.BinaryWriter writer)
{
int nameLength = this.Name.Length;
int lengthdiff = 128 - nameLength;
int totallength = nameLength + lengthdiff;
byte[] arr2 = System.Text.Encoding.UTF8.GetBytes(this.Name.PadRight(totallength, '\0'));
writer.Write(arr2);
}
It doesn't error.
The issue is the way it writes out accented characters.
As looking at the output Sébastian writes out as:
Sébastien
I have tried using BOMUTF8 without any joy, I have tried ASCII.
What does work is using codepage 1252 or Default, and while default is fine for my machine, if someones globalization settings are different (e.g. Asian countries) default won't work.
Also I want to get away with NOT using codepage 1252, call it a pre-requisit!
Any help what I am doing wrong?
Reply
Answers (
1
)
Can we build one exe in another exe in c# .net win app?
Wiki one step login