473,465 Members | 1,651 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 79539
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...
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...
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,...
1
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...
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.