473,774 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getChar s(). 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 3151
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.getChar s(). 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.co m>
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.co m>
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(fi leName,
System.Text.Uni codeEncoding.AS CII);
String s = sr.ReadToEnd();
sr.Close();

for (int i=0;i<3000;i++)
{
Console.WriteLi ne("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.Enc oding
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(fi leName,
System.Text.Uni codeEncoding.AS CII);
UnicodeEncoding .ASCII is a very weird way of writing that - I've seen
people use ASCIIEncoding.A SCII 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.WriteLi ne("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.Enc oding
i can't get it right.
how can i set other encodings, for example this
Cp1252 or Cp437 ?


Use Encoding.GetEnc oding - see the documentation for it for more
information. You might use Encoding.GetEnc oding (1252) for instance, or
Encoding.GetEnc oding(437).

--
Jon Skeet - <sk***@pobox.co m>
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.co m>
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.co m>
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
12988
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 is somehow hanging when relatively large file is used. My original intent was to try to just add a check for file size, and error out somehow if the file was "too" large, but in looking at the
7
2813
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 Col3 Col4 A0012430 REKAL TVÄTTMEDEL EKOMAX 0,5L ST 75.9000
3
3514
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 chars, i can read it in 50 chars for twice or 10 chars in ten times or 25 chars for 4 times...like this. Here, file size, i.e 100 chars is known to me....but if the file size is unknown then how can i read in equal chunks so that at the last call i...
7
7773
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 strReturn += input + vbCrLf input = sr.Read
16
446
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 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
1
5679
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 Fortran to read those files and compile this Fortran extension using F2PY. Now that it seems that no possible combinations of Fortran/C compilers actually *work* with Python 2.4 on Windows XP, I was trying to translate the Fortran subroutine to...
26
4107
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 kind of fun). As I go through the book, I seek to do all the exercises because they are very useful, and good, but it seems like I am just stumbling through somewhat. In particular, I don't really know how to think about "catching errors", or how...
11
3595
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 Read_bin(ByVal ruta As String) Dim cadena As String = "" Dim dato As Array If File.Exists(ruta) = True Then
6
3531
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 to an old program that I wrote in delphi and it's a good opportunity to start with c++.
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.