简体   繁体   English

C#从文本框中将十六进制字符串写为字节

[英]C# Writing hex string from textbox as bytes

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). 我试图将十六进制值从文本框字符串(即ffff)转换为0xffff作为INT(这样,我可以使用二进制编写器将FFFF写入文件中的2个字节)。

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)). (但是同样不确定如何将字符串0002045E转换为int 0x0002045E(作为4个字节))。 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? 如果这不是正确的主意,那么我应该使用什么来将用户在文本框中输入的十六进制值转换 BY BYTES?

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

这很奏效,谢谢m.rogalski!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM