I need a bit of help, How would I go about converting the below to a byte.
string s = "0x01";
byte b = Convert.toByte(s); //(Tried this) ??
byte c = byte.Parse(s); //(Tried this as well)
How would I convert s to a byte ?
I guess the parse function won't allow the prefix 0X in the string so you might use sub-string to remove it.
byte myByte = Byte.Parse(s.SubString(2), NumberStyles.HexNumber);
Or use -
byte myByte = Convert.ToByte(s,16);
int? ToByte also accepts radix.Convert.ToByte("0xef", 16) works correctly.0x and you don't need to convert to an int first.Firstly, remove "0x" from the string value and then use parse() method with NumberStyles.HexNumber
string s = "AA";
byte byteValue = 0;
try
{
byteValue = byte.Parse(s, NumberStyles.HexNumber | NumberStyles.AllowHexSpecifier);
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.WriteLine("String value: 0x{0}, byte value: {1}", s, byteValue);
0x01to a string. Fix it to the actual string you have.0x??or do you need to support binary, octal, decimal as well?