How do I do this in C#?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Im trying to move away from VB to C#.

In VB I can write a line of code that takes a characeter, converts it to an
ASCII number (base10) and then converts that base10 number to hexadecimal by
using the following command

hex(asc("C"))

how do I do this in c#?

You might have guessed that I am using this combination of functions
together with a few other tricks to scamble up a license key or encrypt it.

Thaks in advance

Huw
 
Bonj said:
Try

char c = 'C';
string s = string.Format("{0:x}", (byte)c);

Cast to an int rather than a byte, otherwise you'll lose information if
the character is above unicode 255.
 
sorry int, yes you're right

I can't get away from thinking ASCII is 0 - 127 and unicode is 0-255?
What's the max value of a unicode character then if not 255?
 
Jon Skeet said:
Cast to an int rather than a byte, otherwise you'll lose information if
the character is above unicode 255.

should that be cast to a unsigned short (ushort) - after all that's all of
the precision that char can represent.


rlf

 
Unicode is a multi-byte character set that is 31 bits long. However, it is
normally encoded as two-bytes-per-character or as UTF-8 which allows one
byte per character with the ability of an indicator byte that allows for
non-ASCII characters.

See http://www.cl.cam.ac.uk/~mgk25/unicode.html

--- Nick
 
Bonj said:
sorry int, yes you're right

I can't get away from thinking ASCII is 0 - 127 and unicode is 0-255?
What's the max value of a unicode character then if not 255?

Well, the max value of a Unicode character is actually 0x10ffff. The
max value of a .NET char is 0xffff. Characters above 0xffff are
represented with surrogate pairs.
 
Roy Fine said:
should that be cast to a unsigned short (ushort) - after all that's all of
the precision that char can represent.

You can indeed do that - I just tend to use int as it's a more commonly
used type than ushort. (The result will always be the same, of course.)
 
Back
Top