0

I am trying to convert hex values from a textbox string (ie ffff) to 0xffff as a INT (This way I can use binary writer to write FFFF as 2 bytes in a file).

I actually used this:

string hextoconvert = Convert.ToInt32(textBox1.Text).ToString("X8");

(But again wasn't sure how to convert the string 0002045E to int 0x0002045E (as 4 bytes)). If that isn't the right idea then what should I use to convert hex values that the user puts in a textbox TO BYTES?

Ferdipux
  • 5,116
  • 1
  • 19
  • 33
Alex
  • 35
  • 5
  • 1
    Possible duplicate of [How can I convert a hex string to a byte array?](https://stackoverflow.com/questions/321370/how-can-i-convert-a-hex-string-to-a-byte-array) – Drag and Drop Aug 30 '17 at 08:55
  • I'm going to bed but I'll be on tomorrow morning to check out the suggestions. I hope I can get my program working! :p – Alex Aug 30 '17 at 08:56
  • Depending on usage - you may need to consider if the values need writing "little endian" or "big endian" (i.e. does the 5E get written first or the 00). – PaulF Aug 30 '17 at 08:58
  • Just use `int.Parse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier);` ? But remember to get rid of the forwarding `0x` – mrogal.ski Aug 30 '17 at 08:58
  • 3
    Possible duplicate of [How do you convert a byte array to a hexadecimal string, and vice versa?](https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa) – Tetsuya Yamamoto Aug 30 '17 at 08:59
  • https://stackoverflow.com/a/14332574/6560478 .. there is so many good duplicate for this one! – Drag and Drop Aug 30 '17 at 08:59
  • Thank you so much! how can I make the bytes go in big endian though? – Alex Aug 30 '17 at 17:47

1 Answers1

0
int.Parse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier);

This worked, thanks m.rogalski!

Alex
  • 35
  • 5