473,385 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

reading 8BIT chars from old DOS file

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
Jul 21 '05 #1
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
Jul 21 '05 #2
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
Jul 21 '05 #3
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
Jul 21 '05 #4
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

Jul 21 '05 #5
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
Jul 21 '05 #6
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
Jul 21 '05 #7
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
Jul 21 '05 #8
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
Jul 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
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...
7
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 ...
3
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...
7
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...
16
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...
1
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...
26
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...
11
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...
6
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...
1
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...
0
isladogs
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.