10

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
  • 35,458
  • 16
  • 93
  • 163
mixo_17
  • 127
  • 1
  • 1
  • 8

3 Answers3

27

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
  • 49,493
  • 11
  • 100
  • 118
11

Try this:

var myByte = Byte.Parse("d7", NumberStyles.HexNumber)
Myles McDonnell
  • 12,943
  • 17
  • 66
  • 116
-2

have you tried to use this?

Byte.parse

Joseph Le Brech
  • 6,541
  • 11
  • 49
  • 84