Obtain Unicode value from Character Code?

C

Craig Wagner

I have a situation where I have a series of character codes stored in the
database. In some cases they are the ASCII value of the character, in other
cases they are the > 127 character code value that you use in combination with
the ALT key to enter special characters into a document (e.g. ALT+0147 and
ALT+0148 get you 'smart quotes').

I'm trying to figure out how (or if it's possible) to obtain the Unicode
equivalent of the character code.

I've tried several variations of the following but so far none of them have
worked:

byte charCode = // get value from datatable
Encoding.ASCII.GetString( new byte [] { charCode } );

I also tried simply casting the charCode as a char, but of course that results
in a character using the charCode as a Unicode value, which gets me the wrong
character.

I found an example in JScript that does exactly what I want, but I'm writing in
C#. I only bring this up because it shows me that what I want to do is at least
possible, which leads me to believe there must be a way to do it.

Basically, I'm looking for an equivalent to the JScript charCodeAt method:

http://msdn.microsoft.com/library/en-us/jscript7/html/jsmthcharcodeat.asp

Any suggestions?
 
G

Guest

Hi Craig,

Didn't you try to get that char codes as "char" or "string"?

You can also try "Encoding.UTF8.GetString(...)".
If you'll get this string then [index] should do the job.

Regards

Marcin
 
J

Jon Skeet [C# MVP]

Craig Wagner said:
I have a situation where I have a series of character codes stored in the
database. In some cases they are the ASCII value of the character, in other
cases they are the > 127 character code value that you use in combination with
the ALT key to enter special characters into a document (e.g. ALT+0147 and
ALT+0148 get you 'smart quotes').

Any suggestions?

I *think* you're after Encoding.Default.

However, the database really ought to be able to know how it's stored
them and give you the text in Unicode. What's the type of the column,
what's the database in question, and how are you retrieving the
database?
 
J

Jon Skeet [C# MVP]

Jon Skeet said:
However, the database really ought to be able to know how it's stored
them and give you the text in Unicode. What's the type of the column,
what's the database in question, and how are you retrieving the
database?

Sorry, the last bit of course should be "how are you retrieving the
value".
 
C

Craig Wagner

Jon Skeet said:
I *think* you're after Encoding.Default.

However, the database really ought to be able to know how it's stored
them and give you the text in Unicode. What's the type of the column,
what's the database in question, and how are you retrieving the
database?

The database has stored the value as an integer. The database isn't storing the
character, it's storing the character code (i.e. it's not storing the smart
quote, it's storing 147 as an integer).

The database is SQL Server 2000 and my C# code is calling a stored proc that is
used to populate a DataTable.
 
J

Jon Skeet [C# MVP]

Craig Wagner said:
The database has stored the value as an integer. The database isn't storing the
character, it's storing the character code (i.e. it's not storing the smart
quote, it's storing 147 as an integer).

The database is SQL Server 2000 and my C# code is calling a stored proc that is
used to populate a DataTable.

So it's storing a character as the integer value in an ill-defined
encoding? That doesn't sound like a terribly good idea, to be honest.

Anyway, Encoding.Default *might* do what you want it to, as I said
before - just put the integer value into a byte array as the sole
value, and call Encoding.GetString to get the appropriate string. If
the encoding is subtly different though, you'll have problems.
 
C

Craig Wagner

Marcin Grz?bski said:
Didn't you try to get that char codes as "char" or "string"?

I'm not sure I understand the question. Do you mean from the database? I've only
got an integer in the database, but I could try doing the conversion to
character on the DB side rather than in my code.
You can also try "Encoding.UTF8.GetString(...)".
If you'll get this string then [index] should do the job.

That was one of the permutations I tried. The problem is it expects you to feed
it the Unicode value of the characters in the GetString argument, and I don't
have the Unicode value, that's what I'm trying to get.
I have a situation where I have a series of character codes stored in the
database. In some cases they are the ASCII value of the character, in other
cases they are the > 127 character code value that you use in combination with
the ALT key to enter special characters into a document (e.g. ALT+0147 and
ALT+0148 get you 'smart quotes').

I'm trying to figure out how (or if it's possible) to obtain the Unicode
equivalent of the character code.

I've tried several variations of the following but so far none of them have
worked:

byte charCode = // get value from datatable
Encoding.ASCII.GetString( new byte [] { charCode } );

I also tried simply casting the charCode as a char, but of course that results
in a character using the charCode as a Unicode value, which gets me the wrong
character.

I found an example in JScript that does exactly what I want, but I'm writing in
C#. I only bring this up because it shows me that what I want to do is at least
possible, which leads me to believe there must be a way to do it.

Basically, I'm looking for an equivalent to the JScript charCodeAt method:

http://msdn.microsoft.com/library/en-us/jscript7/html/jsmthcharcodeat.asp

Any suggestions?
---
Craig Wagner, craig.wagner(at)comcast.net
Portland, OR

"Don't ban high-performance vehicles, ban low-performance drivers!"
 
C

Craig Wagner

Jon Skeet said:
So it's storing a character as the integer value in an ill-defined
encoding? That doesn't sound like a terribly good idea, to be honest.

We also have the font set to use to represent the character. This information is
being used to create a Word document using WordML.

It ain't my design, but it has been working pretty well until we went to
implement this new feature.
Anyway, Encoding.Default *might* do what you want it to, as I said
before - just put the integer value into a byte array as the sole
value, and call Encoding.GetString to get the appropriate string. If
the encoding is subtly different though, you'll have problems.

I'll give it a shot. Thanks.
 
C

Craig Wagner

Jon Skeet said:
Anyway, Encoding.Default *might* do what you want it to, as I said
before - just put the integer value into a byte array as the sole
value, and call Encoding.GetString to get the appropriate string. If
the encoding is subtly different though, you'll have problems.

That did it. Worked like a charm. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top