convert ASCII code to Unicode (dos to win)

hamid

Centurion
Joined
Jul 13, 2004
Messages
114
hi,
i have some dos txt files and i want convert all char (in ascii mode) to unicode char and show them to win forms.
how can i convert ascii char to unicode?
ascii code: ù÷‘،¤‘î

thanks
 
Looking up System.Text.Encoding in MSDN, you'll find examples for converting from Unicode to ASCII:

Visual Basic:
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

It should not be too difficult to convert this code so that it performs ASCII to Unicode.
 
Sods law... you post C#, they ask for VB, you post VB, they ask for C#.
You know, as I mentioned in my post, this is all available in MSDN. Give a man a fire, and all that...

C#:
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);
      }
   }
}

Good luck :cool:
 
thanks so much

Code:
            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);
i check it by this asci: ù÷‘،¤‘î
but it return ??????

why? :(
 
Try this... It should be essentially the same as MrPauls example, but I provided both conversion methods.
C#:
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;
		}
	}
}
 
i did it and show result in messagebox but it just return '?'
@cags please chek it by your code (convert ascii code: ù÷‘،¤‘î to unicode)

thx
 
Having thought about it, i'm suspecting that the reason the method doesn't work could be down to the way the .Net Framework views a string. If the .Net Framework uses unicode encoding by default on its string class then storing the ascii you are trying as a string object would simply make it no longer ascii.

Basically what I'm getting at is its not the conversion method itself thats wrong, but the way you are inputting the ascii string. It's hard to explain exactly what I mean, but I think theres something to my thoughts. Either that or my brains just ran off at a tangent as per normal.
 
In an attempt to create a true ASCII string in C# I decided to look up the decimal values of each char in order to create a byte array representing the string. It was in doing this I realised your problem. ASCII is a 7bit encoding meaning that any decimal value above 127 is invalid, in the 'ASCII' you provide several of the chars aren't within this range. They are considered part of the Extended ASCII set, which is to say they have values above 127 and thus aren't really ASCII. Since this is the case it is my belief you will have very little success in using the Encoding.ASCII class to convert that string to what you consider Unicode.
 
As Cags says if the bytes are above 127 then you haven't got ASCII, it is probably ANSI text.
That changes things considerably as ANSI text will also have an associated code-page. Rather than using the ASCII encoder you will need to call the System.Text.Encoding.GetEncoding() method and specify the relevant codepage.
 
ok
how can i get asci code from charachters?

maximum of ascii is 256 but sometimes (int)ch return value more than 1000 !!
please find ascii code for each charachter ù÷‘،¤‘î
 
No, ASCII defines characters in the range 0 to 127 (7 bits). Values in the range 128 to 255 are part of extended ASCII and not handled by the ASCII encoding class. As PlausiblyDamp suggests, manually selecting a codepage is probably your easiest option. As a guess I'd say Windows-1252 would work, but you'd have to try to be sure.
 
i dont know how can i use windows-1252 encoding ?
i just want find ascii code of these charachteres :<
i used utf7 it might be true but returns many chars!
 
Those characters do not have an ASCII value - they are not part of the ASCII character set. http://www.lookuptables.com/ lists the ASCII characters and their values.

As stated previously those characters are probably from an ANSI character set, however without knowing the code-page it is a non-trivial (probably impossible) task to convert them to Unicode.
 
i didi it by
Code:
            Encoding defencd = Encoding.Default;
            byte[] defbyt = defencd.GetBytes(dosstr);
            char[] defchar = defencd.GetChars(defbyt);
...
in defbyt there is ascii codes.
----

if hold ALT and enter 196 it insert a horizontal line.
how can i use 196 to set that line in programming?
i want insert that line in txt file how can i add it by programming?
 
Encoding.Default just uses the ANSI codepage of your PC, this is an almost definate source of problems if you run the application on a PC which uses a different codepage. If uyou know what codepage the characters are from then use the System.Text.Encoding.GetEncoding() method to specify the correct codepage.
 
Back
Top