conversion string[n] to int ?

  • Thread starter Thread starter user
  • Start date Start date
U

user

Hello
i have:
Sting s;
and it's first element is ASCII character which in ASCII code is equal
1. How can i convert it to byte ?
When i tried:
Convert.ToByte(s.substring(0,1),10);
it throught exception because first character in that string is not any
of characters '0'-'9'.

Thanx
 
i have:
Sting s;
and it's first element is ASCII character which in ASCII code is equal
1. How can i convert it to byte ?
When i tried:
Convert.ToByte(s.substring(0,1),10);
it throught exception because first character in that string is not any
of characters '0'-'9'.

If you're asking how to get the value of a character as a byte, you
just cast to byte - but be aware that the top 8 bits will be discarded:

byte x = (byte)(s[0]);
 

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

Back
Top