f1wade
f1wade

Reputation: 2967

converting byte to string and back again

I have a Byte from my database, stored as 0-254

i can convert it from a Byte to string using

byteVal.ToString() //0 return 0 20 returns 20

but to then return it back to a Byte I cannot figure out.

Upvotes: 0

Views: 122

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460380

Similar to other number-types you need the appropriate Parse-method, in this case Byte.Parse:

Byte b = Byte.Parse("20");

If you don't know if the format is valid you can use Byte.TryParse:

Byte b;
if(!Byte.TryParse("256", out b))
    Console.WriteLine("Not a valid byte");

Upvotes: 1

vc 74
vc 74

Reputation: 38179

You can use the Convert.ToByte overload which converts string to a byte

Upvotes: 0

Related Questions