我怎样才能十六进制字符串转换为字节数组? [英] How can I convert a hex string to a byte array?

查看:335
本文介绍了我怎样才能十六进制字符串转换为字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa-in-c\">How你是否转换成字节数组十六进制字符串,反之亦然,在C#中?

我们能一个十六进制字符串转换为字节数组使用C#内置功能或做我必须做出一个自定义的方法呢​​?

Can we convert a hex string to a byte array using a built-in function in C# or do I have to make a custom method for this?

推荐答案

下面是一个不错的乐趣LINQ的例子。

Here's a nice fun LINQ example.

public static byte[] StringToByteArray(string hex) {
    return Enumerable.Range(0, hex.Length)
                     .Where(x => x % 2 == 0)
                     .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                     .ToArray();
}

这篇关于我怎样才能十六进制字符串转换为字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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