字符串转换为十六进制的字符串在C# [英] Convert string to hex-string in C#
本文介绍了字符串转换为十六进制的字符串在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有像样品的字符串。我想它在字符串中的十六进制格式;像这样的:
I have a string like "sample". I want to get a string of it in hex format; like this:
"796173767265"
请给C#语法。
推荐答案
首先,您需要得到它变成一个字节[]
,所以做到这一点:
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.
这篇关于字符串转换为十六进制的字符串在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文