467,915 Members | 1,158 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

How do i convert unicode string to ansi string in C#?


Hi,
I need to convert unicode string to ansi string

Thanks in adavance.
Nov 17 '05 #1
  • viewed: 77048
Share:
4 Replies
use System.Text.Encoding.Convert static method..

--

Thanks,
Yunus Emre ALP÷ZEN
BSc, MCSD.NET

"Julia" <co********@012.net.il> wrote in message
news:uO**************@TK2MSFTNGP10.phx.gbl...

Hi,
I need to convert unicode string to ansi string

Thanks in adavance.

Nov 17 '05 #2
Julia <co********@012.net.il> wrote:
I need to convert unicode string to ansi string


All strings in .NET are Unicode already - there's no such thing as an
ANSI string within .NET. If you need a string encoded into a byte array
as ANSI, you need Encoding.GetBytes.

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
"Julia" <co********@012.net.il> wrote in message
news:uO**************@TK2MSFTNGP10.phx.gbl...
I need to convert unicode string to ansi string


http://www.classdotnet.com/ioCsExamples.html
Nov 17 '05 #4
Mark Rae <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote:
"Julia" <co********@012.net.il> wrote in message
news:uO**************@TK2MSFTNGP10.phx.gbl...
I need to convert unicode string to ansi string


http://www.classdotnet.com/ioCsExamples.html


I hate to say it, but the code on there is pretty awful.

Save byte array to a binary file:
1) No "using" statement to close the file if anything goes wrong.
(It may seem picky to state this for every example, but it's really,
really important.)
2) There's no need for a binary writer if you're just saving a byte
array! Just use Stream.Write.

Read a file into a byte array:
1) Again, no using statement
2) Again, no need for a binary reader
3) Assumes file length doesn't change after finding out length
See http://www.pobox.com/~skeet/csharp/readbinary.html for better code

Write ANSI strings to a text file:
1) No "using" statement
2) Doesn't use ANSI! Uses UTF-8 (the default encoding for StreamWriter)

Read UTF-8 file and display in textbox
1) No using statement
2) No need to explicitly use UTF-8 when that's the default!
3) Loading a file in the UI thread is a bad idea. Why even
bother making the example a GUI one? (The name of the method
is pretty terrible too.)

Writer string to UTF-8 encoded file:
1) No using statement
2) Again, why explicitly state UTF-8? (If it's because the
property is different from the version used by default,
that should be very clearly commented.)

Write Unicode File:
1) No using statement
2) Again, uses UI property while doing file IO. Generally not
a good idea, and again not needed for the example.

Seeking etc
1) No using statement
2) I far prefer the Position property to calling Seek when you're going
to a specific place in the file. I think it's much clearer. That's
possibly just me though.
3) Comment for calling Stream.Read says that 100 bytes are read, when
actually *up to* 100 bytes are read. This should be at least
mentioned, as it's the cause of frequent problems. At least the
return value is stored in a variable...

Copy a file
1) Again, IO in a UI thread...
2) What's wrong with a straight call to File.Copy?

Move/Rename, and Delete
See problems with copy

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Morten Overgaard | last post: by
5 posts views Thread by Terry Olsen | last post: by
2 posts views Thread by Clay | last post: by
4 posts views Thread by Peter | last post: by
3 posts views Thread by =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.