且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

字符串转换为十六进制的字符串在C#

更新时间:2023-02-12 15:18:49

首先,您需要得到它变成一个字节[] ,所以做到这一点:

First you'll need to get it into a byte[], so do this:

byte[] ba = Encoding.Default.GetBytes("sample");



然后就可以得到字符串:

and then you can get the string:

var hexString = BitConverter.ToString(ba);

现在,那将返回与破折号(字符串 - )的,所以你可以然后只需使用这样的:

now, that's going to return a string with dashes (-) in it so you can then simply use this:

hexString = hexString.Replace("-", "");

要摆脱那些如果你想要的。

to get rid of those if you want.

注意::您可以使用不同的 编码 ,如果​​你需要的。

NOTE: you could use a different Encoding if you needed to.