By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 765 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Obtain Unicode value from Character Code?

P: n/a
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...charcodeat.asp

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

"Don't ban high-performance vehicles, ban low-performance drivers!"
Nov 16 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
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
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...charcodeat.asp

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

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

Nov 16 '05 #2

P: n/a
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
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...charcodeat.asp

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

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

Nov 16 '05 #3

P: n/a
Craig Wagner <cr************@hotmail.com> wrote:
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').
<snip>
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?

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

P: n/a
Craig Wagner <cr************@hotmail.com> wrote:
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').
<snip>
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?

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

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
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".

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

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
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".

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

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Craig Wagner <cr************@hotmail.com> wrote:
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').


<snip>
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?


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.
---
Craig Wagner, craig.wagner(at)comcast.net
Portland, OR

"Don't ban high-performance vehicles, ban low-performance drivers!"
Nov 16 '05 #8

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Craig Wagner <cr************@hotmail.com> wrote:
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').


<snip>
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?


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.
---
Craig Wagner, craig.wagner(at)comcast.net
Portland, OR

"Don't ban high-performance vehicles, ban low-performance drivers!"
Nov 16 '05 #9

P: n/a
Craig Wagner <cr************@hotmail.com> wrote:
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.


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.

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

P: n/a
Craig Wagner <cr************@hotmail.com> wrote:
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.


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.

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

P: n/a
Marcin Grz?bski <mg*******@taxussi.no.com.spam.pl> wrote:
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...charcodeat.asp

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

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


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

"Don't ban high-performance vehicles, ban low-performance drivers!"
Nov 16 '05 #12

P: n/a
Marcin Grz?bski <mg*******@taxussi.no.com.spam.pl> wrote:
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...charcodeat.asp

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

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


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

"Don't ban high-performance vehicles, ban low-performance drivers!"
Nov 16 '05 #13

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
>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.


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.
---
Craig Wagner, craig.wagner(at)comcast.net
Portland, OR

"Don't ban high-performance vehicles, ban low-performance drivers!"
Nov 16 '05 #14

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
>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.


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.
---
Craig Wagner, craig.wagner(at)comcast.net
Portland, OR

"Don't ban high-performance vehicles, ban low-performance drivers!"
Nov 16 '05 #15

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
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.
---
Craig Wagner, craig.wagner(at)comcast.net
Portland, OR

"Don't ban high-performance vehicles, ban low-performance drivers!"
Nov 16 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.