8

I have a method which takes a hex value and assign it as a plaintext but type of byte like that

byte plainText = 0xd7;

I want to take this value from textbox ,for exmaple the user will type d7 to textbox and ı will assign it like

byte plaintText = 0xd7

I could not achive that.

oleksii
  • 34,551
  • 12
  • 89
  • 155
mixo_17
  • 107
  • 1
  • 1
  • 7

3 Answers3

25

You can use the Convert.ToByte(String, Int32) method with the base set to 16 (hexadecimal):

String text = "d7";
byte value = Convert.ToByte(text, 16);    
Sebastian Paaske Tørholm
  • 47,464
  • 10
  • 95
  • 116
11

Try this:

var myByte = Byte.Parse("d7", NumberStyles.HexNumber)
Myles McDonnell
  • 12,353
  • 14
  • 61
  • 106
-2

have you tried to use this?

Byte.parse

Joseph Le Brech
  • 6,447
  • 11
  • 46
  • 84