Hi !
I want to load an old Pascal-Dos-File where
records stand in. When i view the file
in a HEX-Editor it's clear how to acces these
Strings and chars in that file. Since these
are old 8BIT chars (C# uses 16BIT) i read
the file bytewise and convert the bytes
to chars using ENCODER.getChars(). From the chars
i make a big String which should be the file as
i see in the HEX-Editor. But there are many errors
in it, as the getChars() Method seem not to
convert all the chars (bytes) propably.
Any suggestions ?
Please Help,
Thanks,
nick 8 3113
Nick <ni**@winger.at> wrote: I want to load an old Pascal-Dos-File where records stand in. When i view the file in a HEX-Editor it's clear how to acces these Strings and chars in that file. Since these are old 8BIT chars (C# uses 16BIT) i read the file bytewise and convert the bytes to chars using ENCODER.getChars(). From the chars i make a big String which should be the file as i see in the HEX-Editor. But there are many errors in it, as the getChars() Method seem not to convert all the chars (bytes) propably. Any suggestions ?
You need to use the right Encoding instance when grabbing the string
(you can usually use GetString rather than GetChars). What encoding was
it written in?
(Alternatively, just use a StreamReader with the appropriate encoding
to start with - you'll still need to know what encoding was used.)
Note that strings are character data, whereas when you're viewing the
file with a hex editor you're viewing it as binary data, possibly with
a text interpretation (in whatever encoding the hex editor feels is
appropriate) available too.
See http://www.pobox.com/~skeet/csharp/unicode.html for more
information about this whole topic.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
hi !
you mentioned this text interpretation of the
hex editor. so in the hex editor every BYTE
is a character and that's exactly what i want.
because in the hex editor i can read all the info
i want to read out of the file, but with c# i
can't convert every byte. can the hex editor check
the encoding, or is this "no" encoding when every byte
is a character.
thanks,
nick
Nick <ni**@winger.at> wrote: you mentioned this text interpretation of the hex editor. so in the hex editor every BYTE is a character and that's exactly what i want. because in the hex editor i can read all the info i want to read out of the file, but with c# i can't convert every byte. can the hex editor check the encoding, or is this "no" encoding when every byte is a character.
The hex editor can't really check the encoding because (say) *every*
file is a valid ISO-8859-1 file, just as an example.
When every byte is a character you still need to interpret that byte as
a character - and that depends on the encoding. The hex editor may well
be assuming an encoding such as Cp1252, or (for an old DOS file) Cp437.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
okay :o)
i have the following code:
StreamReader sr = new StreamReader(fileName,
System.Text.UnicodeEncoding.ASCII);
String s = sr.ReadToEnd();
sr.Close();
for (int i=0;i<3000;i++)
{
Console.WriteLine("Byte "+i+": "+s[i]);
}
and, of course because of the wrong encoding,
i get the following error output:
Byte 0: Byte 86: 244:
Byte 323: 1: te 480: 637: Byte 716: : e 873:
Byte 874:
Byte 875:
it leaves some bytes undecoded.
but with the few encodings in System.Text.Encoding
i can't get it right.
how can i set other encodings, for example this
Cp1252 or Cp437 ?
thanks,
nick
Nick <ni**@winger.at> wrote: okay :o) i have the following code:
StreamReader sr = new StreamReader(fileName, System.Text.UnicodeEncoding.ASCII);
UnicodeEncoding.ASCII is a very weird way of writing that - I've seen
people use ASCIIEncoding.ASCII or UnicodeEncoding.Unicode, but never
mixing the two. I just write Encoding.ASCII, as that's where it's
actually declared.
String s = sr.ReadToEnd(); sr.Close();
for (int i=0;i<3000;i++) { Console.WriteLine("Byte "+i+": "+s[i]); }
and, of course because of the wrong encoding, i get the following error output: Byte 0: Byte 86: 244: Byte 323: 1: te 480: 637: Byte 716: : e 873: Byte 874: Byte 875:
it leaves some bytes undecoded. but with the few encodings in System.Text.Encoding i can't get it right. how can i set other encodings, for example this Cp1252 or Cp437 ?
Use Encoding.GetEncoding - see the documentation for it for more
information. You might use Encoding.GetEncoding (1252) for instance, or
Encoding.GetEncoding(437).
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
hi,
all these getencoding methods seem to work,
i get no exception, but i can't still read
the textfile the way i want ?!?
i've tried it now with the simple notepad
(which could/should be easily implemented)
and it works with ANSI and UTF8. I can
read the few things i would need.
but when i read it with the StreamReader
and use Encoding.UTF8 or ANSI i get the same
not working output as if i would use ascii
or unicode (which are both not displaying
the data the way i would need it in notepad)
what could be so hard for c# reading the file
like notepad does or just interpret every
byte as a ascii character like the hex editor
does ?
thanks,
nick
Nick <ni**@winger.at> wrote: all these getencoding methods seem to work, i get no exception, but i can't still read the textfile the way i want ?!? i've tried it now with the simple notepad (which could/should be easily implemented) and it works with ANSI and UTF8. I can read the few things i would need.
Reading it as UTF8 should give radically different results to reading
it as "ANSI" (where "ANSI" means different things depending on your
region).
but when i read it with the StreamReader and use Encoding.UTF8 or ANSI i get the same not working output as if i would use ascii or unicode (which are both not displaying the data the way i would need it in notepad)
what could be so hard for c# reading the file like notepad does or just interpret every byte as a ascii character like the hex editor does ?
Interpreting every byte as an ASCII character is easy - just use
Encoding.ASCII. You then run into trouble with bytes > 127 though, as
ASCII doesn't have values > 127.
One thing to consider is whether or not you're accurately examining the
data after reading it in. I find the best way to find out exactly what
characters I've got in a string is to print out their Unicode values
one at a time, as numbers, and look them up at http://www.unicode.org
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
nick <ni**@winger.at> wrote: now i have read a lot of articles in the net and tried a lot of things, nothing helped. now i've opened the file with notepad using ANSI encoding and saved it with ending .txt NOW it worked in c#, and i could read the info. so what's the difference ?
Without knowing exactly what you're doing or what's in the file, it's
hard to say.
in explorer both files have the same size. i opened them with hex edit and the ONLY difference is that every "00" hex pair is replaced by "20" hex. hmmm
and that's the problem with c# reading the file ?
I doubt it - but it may be part of the problem with how you were
displaying it.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ohaya |
last post by:
Hi,
I'm a real newbie, but have been asked to try to fix a problem in one of
our JSP pages that is suppose to read in a text file and display it.
From my testing thus far, it appears this page...
|
by: jamait |
last post by:
Hi all,
I m trying to read in a text file into a datatable...
Not sure on how to split up the information though, regex or substrings...?
sample:
Col1 Col2 ...
|
by: syntax |
last post by:
hi, i want to read a file using fread() in equal chunks through a for
loop so that at the last call i dont get error..which way, i should
read it?
let me give an example, suppose i have 100...
|
by: Drew Berkemeyer |
last post by:
Hello,
I'm using the following code to read a text file in VB.NET.
Dim sr As StreamReader = File.OpenText(strFilePath)
Dim input As String = sr.ReadLine()
While Not input Is Nothing...
|
by: Nick |
last post by:
Hi !
I want to load an old Pascal-Dos-File where
records stand in. When i view the file
in a HEX-Editor it's clear how to acces these
Strings and chars in that file. Since these
are old 8BIT...
|
by: Andrea Gavana |
last post by:
Hello NG,
that may sound a silly question, but I didn't find anything really
clear about the issue of reading unformatted big endian files with
Python. What I was doing till now, was using...
|
by: vlsidesign |
last post by:
I am a newbie and going through "The C programming language" by
Kernighan & Richie on my own time (I'm not a programmer but I want to
learn because it can save me time in my normal job, and it is...
|
by: Freddy Coal |
last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all
the file in a String.
I make this function for make that:
'--------------------------
Public Shared Function...
|
by: efrenba |
last post by:
Hi,
I came from delphi world and now I'm doing my first steps in C++. I'm
using C++builder because its ide is like delphi although I'm trying to
avoid the vcl.
I need to insert new features...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |