字节[]以十六进制字符串 [英] byte[] to hex string

查看:167
本文介绍了字节[]以十六进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何转换字节[] 字符串?每次我尝试它,我得到

How do I convert a byte[] to a string? Every time I attempt it, I get

System.Byte []

System.Byte[]

,而不是值

另外,我怎么十六进制值而不是十进制的?

Also, how do I get the value in Hex instead of a decimal?

推荐答案

有一个内置的方法是:

byte[] data = { 1, 2, 4, 8, 16, 32 };

string hex = BitConverter.ToString(data);

结果:01-02-04-08-10-20

Result: 01-02-04-08-10-20

如果你想让它不含破折号,只是将其删除:

If you want it without the dashes, just remove them:

string hex = BitConverter.ToString(data).Replace("-", string.Empty);

结果:010204081020

Result: 010204081020

如果你想要一个更紧凑的再presentation,您可以使用Base64编码:

If you want a more compact representation, you can use Base64:

string base64 = Convert.ToBase64String(data);

结果:AQIECBAg

Result: AQIECBAg

这篇关于字节[]以十六进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆