473,715 Members | 2,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Crazy with character encoding

Hi,
I have a text file with following content:
"((^)|(.* +))§§§§§§§§"

if I read it with:
k=System.IO.Str eamReader( "file.txt",Syst em.Text.Encodin g.ASCII);

k.readtotheend( )

Then "§" is lost

If read it with UTF7

then "+" is lost.

Please help, how can I read the file into string so that I have all
characters?

Thancs
Aug 3 '06 #1
37 3373
Hi Zhiv,

Well, the text isn't ascii or utf7. Most likely it is encoded with the default code table for the location of the file.

Try Encoding.Defaul t or Encoding.GetEnc oding(<write name of encoding here>)

--
Happy coding!
Morten Wennevik [C# MVP]
Aug 3 '06 #2
Zhiv Kurilka <Zh**********@L ozhkaVil.cawrot e:
I have a text file with following content:
"((^)|(.* +))§§§§§§§§"

if I read it with:
k=System.IO.Str eamReader( "file.txt",Syst em.Text.Encodin g.ASCII);

k.readtotheend( )

Then "§" is lost
Yes, because that isn't an ASCII character.
If read it with UTF7

then "+" is lost.
Yes, because it isn't a UTF-7 file. (UTF-7 is very rarely used outside
mail.)
Please help, how can I read the file into string so that I have all
characters?
Well, what encoding is the file in? What created it?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 3 '06 #3
I have created it by hand, it is just a number of characters. I suppose I
nead a bytereader for that. Right?

"Jon Skeet [C# MVP]" <sk***@pobox.co mschrieb im Newsbeitrag
news:MP******** *************** *@msnews.micros oft.com...
Zhiv Kurilka <Zh**********@L ozhkaVil.cawrot e:
I have a text file with following content:
"((^)|(.* +))§§§§§§§§"

if I read it with:
k=System.IO.Str eamReader( "file.txt",Syst em.Text.Encodin g.ASCII);

k.readtotheend( )

Then "§" is lost
Yes, because that isn't an ASCII character.
If read it with UTF7

then "+" is lost.
Yes, because it isn't a UTF-7 file. (UTF-7 is very rarely used outside
mail.)
Please help, how can I read the file into string so that I have all
characters?
Well, what encoding is the file in? What created it?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 3 '06 #4
The question is rather with what did you create it, and how did you save it.
I'm guessing it is saved with the default ansi table for your computer, in which case using Encoding.Defaul t when reading it should give you the proper string.

On Thu, 03 Aug 2006 19:08:48 +0200, Zhiv Kurilka <Zh**********@L ozhkaVil.cawrot e:
I have created it by hand, it is just a number of characters. I suppose I
nead a bytereader for that. Right?

"Jon Skeet [C# MVP]" <sk***@pobox.co mschrieb im Newsbeitrag
news:MP******** *************** *@msnews.micros oft.com...
Zhiv Kurilka <Zh**********@L ozhkaVil.cawrot e:
>I have a text file with following content:
"((^)|(.* +))§§§§§§§§"

if I read it with:
k=System.IO.St reamReader( "file.txt",Syst em.Text.Encodin g.ASCII);

k.readtotheend ()

Then "§" is lost

Yes, because that isn't an ASCII character.
>If read it with UTF7

then "+" is lost.

Yes, because it isn't a UTF-7 file. (UTF-7 is very rarely used outside
mail.)
>Please help, how can I read the file into string so that I have all
characters?

Well, what encoding is the file in? What created it?


--
Happy coding!
Morten Wennevik [C# MVP]
Aug 3 '06 #5
I have created it with Visual Studio text editor. It is a plain text file.
(I suppose). I opened a new text document and wrote those symbols into it.
Encoding.defaul t gives a wrong string.
I am trying byte reader now
"Morten Wennevik" <Mo************ @hotmail.comsch rieb im Newsbeitrag
news:op.tdpw09k 4klbvpo@stone.. .
The question is rather with what did you create it, and how did you save
it.
I'm guessing it is saved with the default ansi table for your computer, in
which case using Encoding.Defaul t when reading it should give you the
proper string.

On Thu, 03 Aug 2006 19:08:48 +0200, Zhiv Kurilka
<Zh**********@L ozhkaVil.cawrot e:
>I have created it by hand, it is just a number of characters. I suppose I
nead a bytereader for that. Right?

"Jon Skeet [C# MVP]" <sk***@pobox.co mschrieb im Newsbeitrag
news:MP******* *************** **@msnews.micro soft.com...
Zhiv Kurilka <Zh**********@L ozhkaVil.cawrot e:
>>I have a text file with following content:
"((^)|(.* +))§§§§§§§§"

if I read it with:
k=System.IO.S treamReader( "file.txt",Syst em.Text.Encodin g.ASCII);

k.readtotheen d()

Then "§" is lost

Yes, because that isn't an ASCII character.
>>If read it with UTF7

then "+" is lost.

Yes, because it isn't a UTF-7 file. (UTF-7 is very rarely used outside
mail.)
>>Please help, how can I read the file into string so that I have all
characters?

Well, what encoding is the file in? What created it?

--
Happy coding!
Morten Wennevik [C# MVP]

Aug 3 '06 #6
A byte reader won't help you as it needs the same kind of encoding as the StreamReader to be able to make sense of the bytes.

My Visual Studio 2005 seems to want to save a text file as Windows-1252, so you can try using that.

StreamReader("f ile.txt", Encoding.GetEnc oding("Windows-1252"));

On Thu, 03 Aug 2006 19:38:44 +0200, Zhiv Kurilka <Zh**********@L ozhkaVil.cawrot e:
I have created it with Visual Studio text editor. It is a plain text file.
(I suppose). I opened a new text document and wrote those symbols into it.
Encoding.defaul t gives a wrong string.
I am trying byte reader now
"Morten Wennevik" <Mo************ @hotmail.comsch rieb im Newsbeitrag
news:op.tdpw09k 4klbvpo@stone.. .
>The question is rather with what did you create it, and how did you save
it.
I'm guessing it is saved with the default ansi table for your computer, in
which case using Encoding.Defaul t when reading it should give you the
proper string.

On Thu, 03 Aug 2006 19:08:48 +0200, Zhiv Kurilka
<Zh**********@ LozhkaVil.cawro te:
>>I have created it by hand, it is just a number of characters. I suppose I
nead a bytereader for that. Right?

"Jon Skeet [C# MVP]" <sk***@pobox.co mschrieb im Newsbeitrag
news:MP****** *************** ***@msnews.micr osoft.com...
Zhiv Kurilka <Zh**********@L ozhkaVil.cawrot e:
I have a text file with following content:
"((^)|(.* +))§§§§§§§§"

if I read it with:
k=System.IO. StreamReader( "file.txt",Syst em.Text.Encodin g.ASCII);

k.readtothee nd()

Then "§" is lost

Yes, because that isn't an ASCII character.

If read it with UTF7

then "+" is lost.

Yes, because it isn't a UTF-7 file. (UTF-7 is very rarely used outside
mail.)

Please help, how can I read the file into string so that I have all
characters ?

Well, what encoding is the file in? What created it?

--
Happy coding!
Morten Wennevik [C# MVP]




--
Happy coding!
Morten Wennevik [C# MVP]
Aug 3 '06 #7
Zhiv Kurilka wrote:
I have created it with Visual Studio text editor. It is a plain text file.
(I suppose). I opened a new text document and wrote those symbols into it.
Encoding.defaul t gives a wrong string.
I am trying byte reader now
Have you tried to read it as UTF8? I think VS saves files in that format.

Max
Aug 3 '06 #8
Some of the files are created using VS2003 others VS2005. I need some way to
get encoding from file automatically. Is it possible?
P.S. I have tried UTF8. For most files it fails.
I am sorry, but I still don't understand what is going on. Why VS editor
shows files properly, but I can't write them?

"Markus Stoeger" <sp******@gmx.a tschrieb im Newsbeitrag
news:OB******** ******@TK2MSFTN GP03.phx.gbl...
Zhiv Kurilka wrote:
>I have created it with Visual Studio text editor. It is a plain text
file. (I suppose). I opened a new text document and wrote those symbols
into it. Encoding.defaul t gives a wrong string.
I am trying byte reader now

Have you tried to read it as UTF8? I think VS saves files in that format.

Max

Aug 3 '06 #9
VS Editor shows files properly because it reads them using the correct encoding.
Have you tried Windows-1252?

On Thu, 03 Aug 2006 20:08:36 +0200, Zhiv Kurilka <Zh**********@L ozhkaVil.cawrot e:
Some of the files are created using VS2003 others VS2005. I need some way to
get encoding from file automatically. Is it possible?
P.S. I have tried UTF8. For most files it fails.
I am sorry, but I still don't understand what is going on. Why VS editor
shows files properly, but I can't write them?

"Markus Stoeger" <sp******@gmx.a tschrieb im Newsbeitrag
news:OB******** ******@TK2MSFTN GP03.phx.gbl...
>Zhiv Kurilka wrote:
>>I have created it with Visual Studio text editor. It is a plain text
file. (I suppose). I opened a new text document and wrote those symbols
into it. Encoding.defaul t gives a wrong string.
I am trying byte reader now

Have you tried to read it as UTF8? I think VS saves files in that format.

Max




--
Happy coding!
Morten Wennevik [C# MVP]
Aug 3 '06 #10

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

Similar topics

4
2031
by: fowlertrainer | last post by:
Hi ! I want to get the WMI infos from Windows machines. I use Py from HU (iso-8859-2) charset. Then I wrote some utility for it, because I want to write it to an XML file. def ToHU(s,NoneStr='-'): if s==None: s=NoneStr if not (type(s) in ):
36
1616
by: Zhiv Kurilka | last post by:
Hi, I have a text file with following content: "((^)|(.* +))§§§§§§§§" if I read it with: k=System.IO.StreamReader( "file.txt",System.Text.Encoding.ASCII); k.readtotheend()
0
8715
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
9322
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9193
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...
0
7966
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
6641
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
5964
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4734
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3171
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
3
2116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.