[C#] Convert a string in ascii to unicode

  • Thread starter Thread starter Guest
  • Start date Start date
Depends on how you "receive a string" but assuming you have a byte array
buffer with your ascii string you can use the Encoding class e.g.

string s = System.Text.Encoding.ASCII.GetString(buffer);

Peter
 
Thanks for your answer Peter.
The method use System.Text.Encoding.ASCII.GetString(buffer);

private string GetPop3String()
{
if(m_socket == null)
{
throw new Pop3MessageException("Connection to POP3 server is closed");
}
byte[] buffer = new byte[MAX_BUFFER_READ_SIZE];
string line = null;
try
{
int byteCount = m_socket.Receive(buffer,buffer.Length,0);
line = Encoding.ASCII.GetString(buffer, 0, byteCount);
}
catch(Exception e)
{
throw new Pop3ReceiveException(e.ToString());
}

return line;
}

My method to convert Ascii to Unicode :
private string ConvertAsciiToUnicode(string theAsciiString)
{
// Create two different encodings.
Encoding aAsciiEncoding = Encoding.ASCII;
Encoding aUnicodeEncoding = Encoding.UTF7;
// Convert the string into a byte[].
byte[] aAsciiBytes = aAsciiEncoding.GetBytes(theAsciiString);
// Perform the conversion from one encoding to the other.
byte[] aUnicodeBytes = Encoding.Convert(aAsciiEncoding, aUnicodeEncoding,
aAsciiBytes);
// Convert the new byte[] into a char[] and then into a string.
char[] aUnicodeChars = new
char[aUnicodeEncoding.GetCharCount(aUnicodeBytes, 0, aUnicodeBytes.Length)];
aUnicodeEncoding.GetChars(aUnicodeBytes, 0, aUnicodeBytes.Length,
aUnicodeChars, 0);
string aUnicodeString = new string(aUnicodeChars);
return aUnicodeString ;
}

I have tested with UTF8 and UTF7 but I have the same result.
What can I do to resolve my problem ?

Best Regards

Freddyboy
 
You don't need ConvertAsciiToUnicode() as it's trying to convert Unicode
string to Unicode string.



Encoding.ASCII.GetString() is doing conversion from ASCII (is it really
ASCII?) to Unicode already.



Do you see square at the end of line? If so, it's probably CR or LF
character.

Another possibility is what bytes in the buffer are not actually ASCII.


Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

*** Want to find answers instantly? Here's how... ***

1. Go to
http://groups-beta.google.com/group/microsoft.public.dotnet.framework.compactframework?hl=en
2. Type your question in the text box near "Search this group" button.
3. Hit "Search this group" button.
4. Read answer(s).

Freddyboy said:
Thanks for your answer Peter.
The method use System.Text.Encoding.ASCII.GetString(buffer);

private string GetPop3String()
{
if(m_socket == null)
{
throw new Pop3MessageException("Connection to POP3 server is closed");
}
byte[] buffer = new byte[MAX_BUFFER_READ_SIZE];
string line = null;
try
{
int byteCount = m_socket.Receive(buffer,buffer.Length,0);
line = Encoding.ASCII.GetString(buffer, 0, byteCount);
}
catch(Exception e)
{
throw new Pop3ReceiveException(e.ToString());
}

return line;
}

My method to convert Ascii to Unicode :
private string ConvertAsciiToUnicode(string theAsciiString)
{
// Create two different encodings.
Encoding aAsciiEncoding = Encoding.ASCII;
Encoding aUnicodeEncoding = Encoding.UTF7;
// Convert the string into a byte[].
byte[] aAsciiBytes = aAsciiEncoding.GetBytes(theAsciiString);
// Perform the conversion from one encoding to the other.
byte[] aUnicodeBytes = Encoding.Convert(aAsciiEncoding,
aUnicodeEncoding,
aAsciiBytes);
// Convert the new byte[] into a char[] and then into a string.
char[] aUnicodeChars = new
char[aUnicodeEncoding.GetCharCount(aUnicodeBytes, 0,
aUnicodeBytes.Length)];
aUnicodeEncoding.GetChars(aUnicodeBytes, 0, aUnicodeBytes.Length,
aUnicodeChars, 0);
string aUnicodeString = new string(aUnicodeChars);
return aUnicodeString ;
}

I have tested with UTF8 and UTF7 but I have the same result.
What can I do to resolve my problem ?

Best Regards

Freddyboy

Peter Foot said:
Depends on how you "receive a string" but assuming you have a byte array
buffer with your ascii string you can use the Encoding class e.g.

string s = System.Text.Encoding.ASCII.GetString(buffer);

Peter
 
Salut,

Maybe this example can help you :

using System;
using System.Text;

namespace TestString
{
class Program
{
static void Main(string[] args)
{
byte[] abcdASCII = new byte[] { 97, 98, 99, 100 };
// 97 = 'a', 98 = 'b', 99 = 'c', 100 = 'd'

UnicodeEncoding ue1 = new UnicodeEncoding();

string abcd_ASC = ue1.GetString(abcdASCII);
// Create a string with "square" characters.

string abcd_UNI = ASCIIToUnicode(abcd_ASC);
}

public static string ASCIIToUnicode(string iASCIIString)
{
UnicodeEncoding ue = new UnicodeEncoding();
byte[] asciiBytes = ue.GetBytes(iASCIIString);
ASCIIEncoding ae = new ASCIIEncoding();
return ae.GetString(asciiBytes);
}
}
}

Hervé.


Freddyboy a écrit :
 
Thanks Hervé,

However I have an error on this line : ae.GetString(asciiBytes);
MethodNotFound

Method is not present in ASCIIEncoding.
Maybe I need to use : ae.ASCII.GetString(asciiBytes);

what is your opinion ?

Thanks


Salut,

Maybe this example can help you :

using System;
using System.Text;

namespace TestString
{
class Program
{
static void Main(string[] args)
{
byte[] abcdASCII = new byte[] { 97, 98, 99, 100 };
// 97 = 'a', 98 = 'b', 99 = 'c', 100 = 'd'

UnicodeEncoding ue1 = new UnicodeEncoding();

string abcd_ASC = ue1.GetString(abcdASCII);
// Create a string with "square" characters.

string abcd_UNI = ASCIIToUnicode(abcd_ASC);
}

public static string ASCIIToUnicode(string iASCIIString)
{
UnicodeEncoding ue = new UnicodeEncoding();
byte[] asciiBytes = ue.GetBytes(iASCIIString);
ASCIIEncoding ae = new ASCIIEncoding();
return ae.GetString(asciiBytes);
}
}
}

Hervé.


Freddyboy a écrit :
Hi,

I use a pop3 class to receive some email with a Windows CE.
Unfortunatly I receive a string with some character like a square.
I think I have a problem with the encodage.
The string that I receive is coded in ascii.
Maybe I need to recode it in unicode.
I have used this link to do it :
http://msdn.microsoft.com/library/d...frlrfsystemtextencodingclassgetcharstopic.asp
However I have the same result.

Somebody have a solution for me please ?

Thanks.
 
Sorry, System.Text.ASCIIEncoding.GetString(...) method doesn't exist on
the Windows CE platform, you can use the following code instead :

using System;
using System.Text;

namespace TestString
{
class Program
{
static void Main(string[] args)
{
byte[] abcdASCII = new byte[] { 97, 98, 99, 100 };
// 97 = 'a', 98 = 'b', 99 = 'c', 100 = 'd'
UnicodeEncoding ue1 = new UnicodeEncoding();

string abcd_ASC = ue1.GetString(abcdASCII);
// Create a string with "square" characters.

string abcd_UNI = ASCIIToUnicode(abcd_ASC);
}

public static string ASCIIToUnicode(string iASCIIString)
{
UnicodeEncoding ue = new UnicodeEncoding();
byte[] asciiBytes = ue.GetBytes(iASCIIString);
byte[] uniBytes = new byte[asciiBytes.Length*2];
for (int nc = 0; nc < asciiBytes.Length; ++nc)
{
uniBytes[nc*2] = asciiBytes[nc];
uniBytes[nc * 2 + 1] = 0;
}
return ue.GetString(uniBytes);
}
}
}

I hope this will work (It works with Framework.NET 2.0 on Windows XP).
Hervé
 
What's wrong with using System.Text.Encoding.ASCII.GetString? It does
exactly what the OP is trying to do and is supported under CF.

so it would look like this:

using System.Text;

static void Main(string[] args)
{
byte[] abcdASCII = new byte[] { 97, 98, 99, 100 };
// 97 = 'a', 98 = 'b', 99 = 'c', 100 = 'd'

string abcd_UNI = ASCII.GetString(abcd_ASC);
}

--
Chris Tacke
Co-founder
OpenNETCF.org
Are you using the SDF? Let's do a case study.
Email us at d c s @ o p e n n e t c f . c o m
http://www.opennetcf.org/donate


Sorry, System.Text.ASCIIEncoding.GetString(...) method doesn't exist on
the Windows CE platform, you can use the following code instead :

using System;
using System.Text;

namespace TestString
{
class Program
{
static void Main(string[] args)
{
byte[] abcdASCII = new byte[] { 97, 98, 99, 100 };
// 97 = 'a', 98 = 'b', 99 = 'c', 100 = 'd'
UnicodeEncoding ue1 = new UnicodeEncoding();

string abcd_ASC = ue1.GetString(abcdASCII);
// Create a string with "square" characters.

string abcd_UNI = ASCIIToUnicode(abcd_ASC);
}

public static string ASCIIToUnicode(string iASCIIString)
{
UnicodeEncoding ue = new UnicodeEncoding();
byte[] asciiBytes = ue.GetBytes(iASCIIString);
byte[] uniBytes = new byte[asciiBytes.Length*2];
for (int nc = 0; nc < asciiBytes.Length; ++nc)
{
uniBytes[nc*2] = asciiBytes[nc];
uniBytes[nc * 2 + 1] = 0;
}
return ue.GetString(uniBytes);
}
}
}

I hope this will work (It works with Framework.NET 2.0 on Windows XP).
Hervé
 
I still have error on my PDA.
Application compile with succes but when I want execute it on the pda I have
the error methoNotFound on GetString().

I don't know where is the problem.

Freddyboy
 
Back
Top