如何将字符串转换为十六进制字节数组? [英] How to convert a String to a Hex Byte Array?

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

问题描述

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

有关测试我的加密算法,我已经被设置按键,纯文本和他们得到的密文。

For testing my encryption algorithm I have being provided keys, plain text and their resulting cipher text.

键和明文是字符串

我如何将其转换为十六进制字节数组??

How do i convert it to a hex byte array??

事情是这样的: E8E9EAEBEDEEEFF0F2F3F4F5F7F8F9FA

要像这样:

byte[] key = new byte[16] { 0xE8, 0xE9, 0xEA, 0xEB, 0xED, 0xEE, 0xEF, 0xF0, 0xF2, 0xF3, 0xF4, 0xF5, 0xF7, 0xF8, 0xF9, 0xFA} ;

感谢名单提前:)

Thanx in advance :)

推荐答案

<一个href=\"http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa-in-c/311179#311179\">Do你需要的这个

static class HexStringConverter
{
    public static byte[] ToByteArray(String HexString)
    {
        int NumberChars = HexString.Length;
        byte[] bytes = new byte[NumberChars / 2];
        for (int i = 0; i < NumberChars; i += 2)
        {
            bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);
        }
        return bytes;
    }
}

希望它帮助。

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

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