473,406 Members | 2,273 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,406 software developers and data experts.

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
4 79524
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Morten Overgaard | last post by:
Hi How do I convert a managed System::String to a CString. I need to handle unicode characters ( Japan and Chinese ) as well as "normal" ANSI characters. I have tried the following char* p...
3
by: nicksop | last post by:
How Can I convert ANSI to OEM with StreamWriter ?
5
by: Terry Olsen | last post by:
Looking for info on how to convert a byte array to a string, and string to byte array. Thanks.
2
by: Clay | last post by:
I need to convert a unicode string to an ansi string. This should not be difficult but I simply cannot find a way to do this. Anyone?
1
by: recover | last post by:
#include <xxx> int main() { const wchar* pwcHello=L"hello"; char* pcHello; xxxxxx //do something using stl cout<<pcHello<<endl; } =============out===========
1
codexparse
by: codexparse | last post by:
I am writing a browser program in C++ Builder 6 that loads a web page using the following code: void __fastcall TForm1::ToolButton1Click(TObject *Sender) { wchar_t buff; ...
4
by: Peter | last post by:
Does anyone know how to convert the following VB6 code to C# code? Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, Source As Any, ByVal bytes As Long) Dim...
3
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, i have a byte that holds a bunch of data of type REG_MULTI_SZ in UNICODE,. I must convert this byte to a REG_MULTI_SZ in ANSI code. How can i do this,...? TIA,... Regards
7
by: firepol | last post by:
Hello there, I am dealing with files encoded in UTF8 and I can't find a way to convert them into ANSI. I've already searched in google for this since a while, and I'm not achieving the result I...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.