Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, August 30, 2017 8:19 AM
I have recycled and modified some of my old Qt code to get the translation function working in my application. It took me about 1.5 hour and it works like a charm except.... the chinese and thai characters.
I have 2 print functions which let the text appear on the display.
private void Print(String S)
{
for (int i = 0; i < S.Length; i++)
{
monitors[index1].GetControlFromPosition(column[index1], row[index1]).ForeColor = color[colorIndex];
monitors[index1].GetControlFromPosition(column[index1]++, row[index1]).Text = S[i].ToString();
}
}
private void PrintUTF(String S)
{ Console.WriteLine("hello world " + S); // does displays this
int UTF;
UTF = Convert.ToInt32(S); // converts the string to an integer value <-- seems to cause the crash Console.WriteLine(S + " " + UTF); // doesn't come here
monitors[index1].GetControlFromPosition(column[index1], row[index1]).ForeColor = color[colorIndex];
monitors[index1].GetControlFromPosition(column[index1]++, row[index1]).Text = UTF.ToString();
}
The first print function is fine, the 2nd does not work. This is the Console output when the language is set to chinese.
<< 0x85 // start of translation
incomming translation
<< 0x20
<< 0x20
<< 0x20
<< 0x20
<< 0x20
<< 0x53
<< 0x53
<< 0x50
<< 0x20
<< 0x50
<< 0x48
<< 0x41
<< 0x53
'MMI_WFA.vshost.exe' (CLR v4.0.30319: MMI_WFA.vshost.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\mscorlib.resources\v4.0_4.0.0.0_nl_b77a5c561934e089\mscorlib.resources.dll'. Module was built without symbols.
Exception thrown: 'System.FormatException' in mscorlib.dll // unsure why I get this halfway in a translation
Exception thrown: 'System.FormatException' in System.Windows.Forms.dll
<< 0x45
<< 0x20
<< 0x36
<< 0x20
<< 0x20
<< 0x20
<< 0x20
<< 0x20
<< 0x30
<< 0x3D
<< 0x45
<< 0x4E
<< 0x44
<< 0x86 // end of translation
Text to translate : SSP PHASE 6 0=END
hello world 32 32 32 32 32 32 83 83 80 32 80 72 65 83 69 32 54 32 32 32 32 32 48 61 32467 26463
The unicode string of numbers is correct!
Set to a conventional West european language the output is
incomming translation
<< 0x20
<< 0x20
<< 0x20
<< 0x20
<< 0x20
<< 0x54
<< 0x45
<< 0x41
<< 0x43
<< 0x48
<< 0x20
<< 0x49
<< 0x4E
<< 0x20
<< 0x41
<< 0x20
<< 0x4E
<< 0x45
<< 0x57
<< 0x20
<< 0x57
<< 0x48
<< 0x45
<< 0x45
<< 0x4C
<< 0x2
Text to translate : TEACH IN A NEW WHEEL
No errors and the correct translation appears on the screen.
Only the Chinese, Arabic and Thai languages make use of the printUTF function.
All replies (9)
Wednesday, August 30, 2017 1:48 PM ✅Answered
I only used Console because that is what YOUR original code example used so it was reasonable to assume you were using a Console application to experiment with. I continued to use that to demonstrate the basic concept of converting the integer code point into a Unicode char.
Perhaps you need to take a step back and learn some basic programming concepts if you cannot extrapolate what you need to do from a general example.
You have only now just said that 32457 is starting off as a string (before that you just said you had an integer 32457).
So...
string exampleStr = "32457";
// convert to an actual number
int i = Convert.ToInt32(exampleStr);
// Typecast that to a char
char c = (char)i;
// Put into a textLabel.
textLabel.Text = c.ToString();
Wednesday, August 30, 2017 8:33 AM
What are you trying to achieve here?
The Convert.Int32() method is for converting a string representation of a number into an actual number.
For example:
string s = "123";
int i = Convert.ToInt32(s);
// i now equals 123
If you try to use Convert.ToInt32 on a string that does not contain a string representation of a number (like "Dog" or "狗") then it will throw a FormatException as you are seeing.
If you are looking for the Unicode numbers/code points for a given string then you could use the UnicodeEncoding.GetBytes method to convert the string to a sequence of bytes then output those bytes as in the example in that link.
Wednesday, August 30, 2017 12:01 PM
Thank you for your help. I don't have it working yet. I am having trouble with the vague syntax.
I split the string into a string array UTF. This array holds the correct numbers. 32 32 50 etc
I am unsure how to use the .GetBytes method. I am also not getting why I want to use getBytes method because it returns a useless byte array. A chinese char unicode value is 32000-ish so this and a byte array don't go along.
private void PrintUTF(String S)
{
String[] UTF = S.Split(' ');
Console.WriteLine(S);
foreach (var word in UTF)
{
byte[] bytes = u16LE.GetBytes(word.ToString()); // I really do not understand what to do with the byte[] array
Console.WriteLine(word, u16LE); // displays the individual numbers
foreach (var letter in bytes)
{
Console.WriteLine(letter.ToString(), u16LE); // I wanted this to be in chinese.... but it cannot possibly work with one byte...
}
//monitors[index1].GetControlFromPosition(column[index1], row[index1]).ForeColor = color[colorIndex];
//monitors[index1].GetControlFromPosition(column[index1]++, row[index1]).Text = bytes[0].ToString();
}
}
In the example code there it this function
public static void PrintHexBytes( byte[] bytes ) {
if (( bytes == null ) || ( bytes.Length == 0 ))
Console.WriteLine( "<none>" );
else {
for ( int i = 0; i < bytes.Length; i++ )
Console.Write( "{0:X2} ", bytes[i] );
Console.WriteLine();
}
}
I know that this propably the way, but I don't know with what I must replace
"{0:X2} "
What is the solution to this puzzel?
P.S.
string s = "123";
int i = Convert.ToInt32(s);
// i now equals 123
I already knew this, I needed to use this in Qt for doing that what I am trying to do now. In Qt there is besides a char also a QChar and if I did QChar(UTF) where UTF is an integer of a unicode character I would get the correct character.
Wednesday, August 30, 2017 12:46 PM
I'm still a bit confused as to what your end-result is intended to be!
If you want to convert a char to its Unicode code point you can just cast to an int.
You can then output that int (as hex) to the console.
e.g.
string s = "狗猫";
foreach (char c in s)
{
Console.WriteLine(String.Format("{0:x2}", (int)c));
}
If you want to display Chinese (or Thai etc) characters in the console directly, that's...sort of easy in that you just output it, but complicated by the fact that the Console is not a fully Unicode interface so this will only display correctly if the PC is in the appropriate code page (as noted here)
Wednesday, August 30, 2017 12:50 PM
I have a textlabel big enough for one character and I have an integer which value is 32467.
That integer needs to be 'transformed' in whatever chinese symbol it stands for and be displayed in the text label
Wednesday, August 30, 2017 12:56 PM
Ah, then it's just the opposite way around!
To convert the number back into a char you can just cast back to a char (see the bolded line):
foreach (char c in s)
{
// convert a character to its unicode value
int codePoint = (int) c;
Console.WriteLine(String.Format ("{0:x2}", codePoint));
// convert a unicode value to its Unicode character
char unicodeChar = (char) codePoint;
// This will not output non-English characters correctly due to Console limitations
// But you should be able to set text of a TextLabel in a WPF or Windows Forms app with no problem
Console.WriteLine(String.Format("{0}", unicodeChar));
}
With this technique, (char)32457 converts to '结'. I can't actually read Chinese so I will just assume this is right :)
Wednesday, August 30, 2017 1:41 PM
Ah, then it's just the opposite way around!
To convert the number back into a char you can just cast back to a char (see the bolded line):
foreach (char c in s) { // convert a character to its unicode value int codePoint = (int) c; Console.WriteLine(String.Format ("{0:x2}", codePoint)); // convert a unicode value to its Unicode character char unicodeChar = (char) codePoint; // This will not output non-English characters correctly due to Console limitations // But you should be able to set text of a TextLabel in a WPF or Windows Forms app with no problem Console.WriteLine(String.Format("{0}", unicodeChar)); }With this technique, (char)32457 converts to '结'. I can't actually read Chinese so I will just assume this is right :)
Mate, no offense but I am still totally nowhere and I've been working on this over half a day. And this should not have taken more than an hour
First I Just said TEXTLABEL.... litterly So plz no more console
secondly you suddenly start use this: foreach (char c in S) with
"狗猫";
I don't have this, this is exactly what I am trying to get all day.
I have a string which is "32457 " and I can convert it into an integer. That is the only thing I can.
I tried the following and it gives me the wrong character.
textlabel.Text = "\u32457"; // <<-- textlabel
On top of that I also cannot concatenate "\u"+ "32457" because \u is an unknown escape sequence. So I cannot produce "\u32457"; for as far as I know. I tried this (@"\u" + "32457 "); but this results in just the '\
So as you see, I am still absolutely nowhere.
So I ask again. How can I process the string or integer 32457 as a chinese char and stuff it in a textlabel,
Wednesday, August 30, 2017 2:03 PM
"You have only now just said that 32457 is starting off as a string (before that you just said you had an integer 32457)"
The very first thing you said to me was:
What are you trying to achieve here?
The Convert.Int32() method is for converting a string representation of a number into an actual number.
For example:
string s = "123";
int i = Convert.ToInt32(s);
// i now equals 123
So how could you possibly not have known that I started with a string?
"Perhaps you need to take a step back and learn some basic programming concepts if you cannot extrapolate what you need to do from a general example."
Before you can say things like this you should learn plain english instead because it is you who failed to comprehend and respond appropiately to this:
"
I have a textlabel big enough for one character and I have an integer which value is 32467.
That integer needs to be 'transformed' in whatever chinese symbol it stands for and be displayed in the text label
"
I litterly said this, and your respons to it was totally not helpful. I will mark your last post as the answer and leave it there. Good bye
Wednesday, August 30, 2017 2:42 PM
I litterly said this, and your respons to it was totally not helpful. I will mark your last post as the answer and leave it there. Good bye
I apologise for my post coming off rather more abrupt than I intended! Glad you got your answer.