Convert Ascii Character to decimal

G

Guest

Hi,

I am reading from a serial port from a device which sends over an ascii
character. It uses the full extended 256 character set and therefore has
non-printing characters too. So what i want to do is convert the input to the
associated decimal.

I.E
ASCII Char 'NUL' will become 0
ASCII Char 'SOH' will become 1
ASCII Char 'STX' will become 2


ASCII Char '0' will become 48
ASCII Char '1' will become 49

All the way to up to the decimal value 255

I know it must be some thing simple but I can't find it! I'm using C#.net or
VB.net
Thanks in advance
 
J

Jon Skeet [C# MVP]

Director - Minvent said:
I am reading from a serial port from a device which sends over an ascii
character. It uses the full extended 256 character set and therefore has
non-printing characters too.

ASCII doesn't have 256 characters.
So what i want to do is convert the input to the
associated decimal.

I.E
ASCII Char 'NUL' will become 0
ASCII Char 'SOH' will become 1
ASCII Char 'STX' will become 2


ASCII Char '0' will become 48
ASCII Char '1' will become 49

All the way to up to the decimal value 255

I know it must be some thing simple but I can't find it! I'm using C#.net or
VB.net

If you want to go up to 256, you need to know what character encoding
it's using for 128-256. For 0-127, you can just cast the character to
an int.
 
N

Nick Malik [Microsoft]

Is it sending over a character or a byte? If it is a byte, then you don't
have to convert to a number... it already is a number. If it is sending a
character, then somewhere along the way, the binary value was mapped to a
code table to get a character. You'd need to map it back!

What mechanism are you using to read the byte? a COM component perhaps?

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 

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