473,395 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

simple unicode question

Hi -

It's funny ... this works fine:

private void button2_Click(object sender, System.EventArgs e) {
string s = "\u0075";
char c = Convert.ToChar(s);
label1.Text = c.ToString();
}
But now imagine I want to build the string s dynamically, e.g.

private void button2_Click(object sender, System.EventArgs e) {
string s1 = "00";
string s2 = "75";
string s = "\u" + s1 + s2;
char c = Convert.ToChar(s);
label1.Text = c.ToString();
}

The code as written above gives me a design-time error under "\u" which says
"Unrecognised escape sequence". If I change "\u" to @"\u" I now get the
following runtime exception:
An unhandled exception of type 'System.FormatException' occurred in
mscorlib.dll.

Additional information: String must be only one character long.

Can anybody please tell me how I can workaround this to dynamically build a
string (like 's') and have it converted into a Unicode character?

Thanks,

Chris.
Nov 15 '05 #1
3 1749
Hope this helps...

static void Main(string[] args)
{
// The inputs
string s1 = "00";
string s2 = "75";

// Now the conversion
short unicodeVal = System.Convert.ToInt16(s1 + s2, 16);
string unicodeCharWithinString = "" + System.Convert.ToChar(unicodeVal);

System.Console.WriteLine(unicodeCharWithinString);
System.Console.ReadLine();
}

JPRoot

"Christopher Ireland" <ch*********@removemee.irelandlife.net> wrote in
message news:OM**************@TK2MSFTNGP12.phx.gbl...
Hi -

It's funny ... this works fine:

private void button2_Click(object sender, System.EventArgs e) {
string s = "\u0075";
char c = Convert.ToChar(s);
label1.Text = c.ToString();
}
But now imagine I want to build the string s dynamically, e.g.

private void button2_Click(object sender, System.EventArgs e) {
string s1 = "00";
string s2 = "75";
string s = "\u" + s1 + s2;
char c = Convert.ToChar(s);
label1.Text = c.ToString();
}

The code as written above gives me a design-time error under "\u" which says "Unrecognised escape sequence". If I change "\u" to @"\u" I now get the
following runtime exception:
An unhandled exception of type 'System.FormatException' occurred in
mscorlib.dll.

Additional information: String must be only one character long.

Can anybody please tell me how I can workaround this to dynamically build a string (like 's') and have it converted into a Unicode character?

Thanks,

Chris.

Nov 15 '05 #2
Christopher Ireland <ch*********@removemee.irelandlife.net> wrote:
It's funny ... this works fine:

private void button2_Click(object sender, System.EventArgs e) {
string s = "\u0075";
char c = Convert.ToChar(s);
label1.Text = c.ToString();
}
Note that the middle line would be more simply written as

char c = s[0];
But now imagine I want to build the string s dynamically, e.g.

private void button2_Click(object sender, System.EventArgs e) {
string s1 = "00";
string s2 = "75";
string s = "\u" + s1 + s2;
char c = Convert.ToChar(s);
label1.Text = c.ToString();
}

The code as written above gives me a design-time error under "\u" which says
"Unrecognised escape sequence".
Absolutely. The "\uxxxx" business belongs to the compiler, not the
runtime. It's the same as trying to do:

string s = "\" + "n";

and expecting to get a newline character.
If I change "\u" to @"\u" I now get the
following runtime exception:
An unhandled exception of type 'System.FormatException' occurred in
mscorlib.dll. ? Additional information: String must be only one character long.

Can anybody please tell me how I can workaround this to dynamically build a
string (like 's') and have it converted into a Unicode character?


Well, what do you *actually* want to get the data as? Do you really
have it as 2 2-character strings? If so, you'd use Byte.Parse twice
(using the form which allows you to specify that they're hex numbers),
shift the top byte left, and then or the results:

char c = (char)((topByte << 8)|lowByte);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
Hi Jon,

Many thanks -- that was the ticket!!

Chris.
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: sebastien.hugues | last post by:
Hi I would like to retrieve the application data directory path of the logged user on windows XP. To achieve this goal i use the environment variable APPDATA. The logged user has this name:...
9
by: François Pinard | last post by:
Hi, people. I hope someone would like to enlighten me. For any application handling Unicode internally, I'm usually careful at properly converting those Unicode strings into 8-bit strings before...
3
by: Roger Thornhill | last post by:
Hi - I have a question that I am sure is a basic UNICODE question for anyone out there with UNICODE experience. I simply would like to see a non-Latin unicode character printed to my console....
8
by: pagates | last post by:
Hello, I am playing a little with Encoding, and I have what is possibly (forgive me) a newbie-type question. I have a function that takes a string and a codepage (based upon the basic MSDN...
14
by: abhi147 | last post by:
Hi , I want to convert an array of bytes like : {79,104,-37,-66,24,123,30,-26,-99,-8,80,-38,19,14,-127,-3} into Unicode character with ISO-8859-1 standard. Can anyone help me .. how should...
12
by: Benry | last post by:
I have two machines. Both have Windows xp Pro. Both have Visual Studio 6.0. These two versions differ only in Forms3 version. Machine 1 (M1) has version 11.0.6254, Machine 2 (M2) has version...
5
by: =?Utf-8?B?S2V2aW4gVGFuZw==?= | last post by:
In MFC, CRichEditCtrl contrl, I want to set the codepage for the control to Unicode. I used the following method to set codepage for it (only for ANSI or BIG5, etc, not unicode). How should I...
1
by: =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?= | last post by:
Hi, A while ago I asked a question on the list about a simple eval function, capable of eval'ing simple python constructs (tuples, dicts, lists, strings, numbers etc) in a secure manner:...
0
by: deloford | last post by:
Hi This is going to be a question for anyone who is an expert in C# Text Encoding. My situation is this: I have a Sybase database which is firing back ISO-8559 encoded strings. I am unable to...
14
by: Russell E. Owen | last post by:
I have code like this: except Exception, e: self.setState(self.Failed, str(e)) which fails if the exception contains a unicode argument. I did, of course, try unicode(e) but that fails. The...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.