Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

[ once4ever ]
Posted

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

 

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.

Never trouble another for what you can do for yourself.
Posted

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...

 

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:

Never trouble another for what you can do for yourself.
Posted

thanks so much

 

           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? :(

[ once4ever ]
Posted

Try this... It should be essentially the same as MrPauls example, but I provided both conversion methods.

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;
	}
}
}

Anybody looking for a graduate programmer (Midlands, England)?
Posted

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.

Anybody looking for a graduate programmer (Midlands, England)?
Posted
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.
Anybody looking for a graduate programmer (Midlands, England)?
  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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 ù÷�،¤�î

[ once4ever ]
Posted
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.
Never trouble another for what you can do for yourself.
  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

i didi it by

            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?

[ once4ever ]
  • Administrators
Posted
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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...