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

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

问题描述

如何将 byte[] 转换为 string?每次我尝试它时,我都会得到

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

如果你想要更紧凑的表示,你可以使用 Base64:

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

string base64 = Convert.ToBase64String(data);

结果:AQIECBAg

Result: AQIECBAg

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

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