HighTechTalks DotNet Forums  

Re: UTF8 to ASCII

Dotnet Academic General Discussions microsoft.public.dotnet.academic


Discuss Re: UTF8 to ASCII in the Dotnet Academic General Discussions forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Peter van der Goes
 
Posts: n/a

Default Re: UTF8 to ASCII - 01-08-2004 , 11:09 AM







"a52" <wabnel (AT) hotmail (DOT) com> wrote

Hello.

I have a problem working with file with VB.NET.
I consurting a DataBase, de Data have special caracters (ñ,
Ñ, etc). When I try to save the file as ASCII File this
characters changed by others.

<snip>
Only the UTF8 file have the data fine, the ASCII changed
don't now why. How can I Make a ASCII file with fine data?

please help me.

Checking the help in Visual Studio .NET, under the Encoding class, there is
an example of converting from Unicode to ASCII. The supplied code may help
you.

[Visual Basic]
Imports System
Imports System.Text

Namespace Convert_Example
Class MyConvertExampleClass
Shared Sub Main()
Dim unicodeString As String = "This string contains the unicode
character Pi(Ð)"

' Create two different encodings.
Dim ascii As Encoding = Encoding.ASCII
Dim [unicode] As Encoding = Encoding.Unicode

' Convert the string into a byte[].
Dim unicodeBytes As Byte() = [unicode].GetBytes(unicodeString)

' Perform the conversion from one encoding to the other.
Dim asciiBytes As Byte() = Encoding.Convert([unicode], ascii,
unicodeBytes)

' Convert the new byte[] into a char[] and then into a string.
' This is a slightly different approach to converting to
illustrate
' the use of GetCharCount/GetChars.
Dim asciiChars(ascii.GetCharCount(asciiBytes, 0,
asciiBytes.Length)) As Char
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
Dim asciiString As New String(asciiChars)

' Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString)
Console.WriteLine("Ascii converted string: {0}", asciiString)
End Sub
End Class
End Namespace

Of course, if your source contains characters that are not in the ASCII
character set, a "fine" conversion is not possible.
I recommend that you read the help article on the Encoding class, especially
the parts on what characters can be converted.


--
Peter [MVP Academic]




Reply With Quote
  #2  
Old   
a52
 
Posts: n/a

Default Re: UTF8 to ASCII - 01-12-2004 , 10:57 AM






I did that.

I found the solutions saving the file with
FileSystemObject.


greeting peter.


Quote:
-----Original Message-----

"a52" <wabnel (AT) hotmail (DOT) com> wrote in message
news:0c2c01c3d5f8$f0c5b3c0$a401280a (AT) phx (DOT) gbl...
Hello.

I have a problem working with file with VB.NET.
I consurting a DataBase, de Data have special caracters
(ñ,
Ñ, etc). When I try to save the file as ASCII File this
characters changed by others.

snip
Only the UTF8 file have the data fine, the ASCII changed
don't now why. How can I Make a ASCII file with fine
data?

please help me.

Checking the help in Visual Studio .NET, under the
Encoding class, there is
an example of converting from Unicode to ASCII. The
supplied code may help
you.

[Visual Basic]
Imports System
Imports System.Text

Namespace Convert_Example
Class MyConvertExampleClass
Shared Sub Main()
Dim unicodeString As String = "This string
contains the unicode
character Pi(Ð)"

' Create two different encodings.
Dim ascii As Encoding = Encoding.ASCII
Dim [unicode] As Encoding = Encoding.Unicode

' Convert the string into a byte[].
Dim unicodeBytes As Byte() =
[unicode].GetBytes(unicodeString)

' Perform the conversion from one encoding
to the other.
Dim asciiBytes As Byte() = Encoding.Convert
([unicode], ascii,
unicodeBytes)

' Convert the new byte[] into a char[] and
then into a string.
' This is a slightly different approach to
converting to
illustrate
' the use of GetCharCount/GetChars.
Dim asciiChars(ascii.GetCharCount
(asciiBytes, 0,
asciiBytes.Length)) As Char
ascii.GetChars(asciiBytes, 0,
asciiBytes.Length, asciiChars, 0)
Dim asciiString As New String(asciiChars)

' Display the strings created before and
after the conversion.
Console.WriteLine("Original string: {0}",
unicodeString)
Console.WriteLine("Ascii converted string:
{0}", asciiString)
End Sub
End Class
End Namespace

Of course, if your source contains characters that are
not in the ASCII
character set, a "fine" conversion is not possible.
I recommend that you read the help article on the
Encoding class, especially
the parts on what characters can be converted.


--
Peter [MVP Academic]



.


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.