Imports System
Imports System.Text
Imports Microsoft.VisualBasic
Namespace Convert_Example
Class MyConvertExampleClass
Shared Sub Main()
Dim unicodeString As String = "This string contains the unicode character Pi(" & ChrW(&H03A0) & ")"
' 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
using System;
using System.Text;
namespace ConvertExample
{
class ConvertExampleClass
{
static void Main()
{
string unicodeString = "This string contains the unicode character Pi(\u03a0)";
// Create two different encodings.
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;
// Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(unicodeString);
// Perform the conversion from one encoding to the other.
byte[] asciiBytes = 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.
char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string asciiString = 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);
}
}
}
Encoding asciienc = Encoding.ASCII;
Encoding unicodenc = Encoding.Unicode;
byte [] asciibyt = asciienc.GetBytes(textBox1.Text);
byte[] unibyt = Encoding.Convert(asciienc, unicodenc, asciibyt);
char[] unichar = new char[unicodenc.GetCharCount(unibyt, 0, unibyt.Length)];
unicodenc.GetChars(unibyt, 0, unibyt.Length, unichar, 0);
string unistr = new string(unichar);
MessageBox.Show(unistr);
using System;
using System.Text;
namespace Convert_Example
{
public class MyConvertExampleClass
{
public static string ConvertAsciiToUnicode(string asciiString)
{
// Create two different encodings.
Encoding encAscii = Encoding.ASCII;
Encoding encUnicode = Encoding.Unicode;
// Convert the string into a byte[].
byte[] asciiBytes = encAscii.GetBytes(asciiString);
// Perform the conversion from one encoding to the other.
byte[] unicodeBytes = Encoding.Convert(encAscii, encUnicode, asciiBytes);
// 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.
char[] unicodeChars = new char[encUnicode.GetCharCount(unicodeBytes, 0, unicodeBytes.Length)];
encUnicode.GetChars(unicodeBytes, 0, unicodeBytes.Length, unicodeChars, 0);
string unicodeString = new string(unicodeChars);
// Return the new unicode string
return unicodeString;
}
public static string ConvertUnicodeToAscii(string unicodeString)
{
// Create two different encodings.
Encoding encAscii = Encoding.ASCII;
Encoding encUnicode = Encoding.Unicode;
// Convert the string into a byte[].
byte[] unicodeBytes = encUnicode.GetBytes(unicodeString);
// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(encUnicode, encAscii, 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.
char[] asciiChars = new char[encAscii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
encAscii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string asciiString = new string(asciiChars);
// Return the new ascii string
return asciiString;
}
}
}
Encoding defencd = Encoding.Default;
byte[] defbyt = defencd.GetBytes(dosstr);
char[] defchar = defencd.GetChars(defbyt);
...