简体   繁体   English

C#将十六进制字符串数组转换为字节数组

[英]C# Convert Hex String Array to Byte Array

I have a String[] of hex values "10" "0F" "3E" "42" stored. 我有一个十六进制"10" "0F" "3E" "42"String[]

I found this method to convert to a Byte[] 我发现此方法可以转换为Byte[]

    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;
    }

However this converts the values to the hex equivalent. 但是,这会将值转换为十六进制等效值。 But the values are already in the hex equivalent! 但是这些值已经等于十六进制

For example this makes "10" "0F" "3E" "42" into "16" "15" "62" "66" . 例如,这使"10" "0F" "3E" "42"变为"16" "15" "62" "66"

I want it to directly copy the values as they are already the correct hex value. 我希望它直接复制值,因为它们已经是正确的十六进制值。

Edit: 编辑:

Basically... 基本上...

I want a byte array with the literal characters in the String[] So say the second value in String[] is 0F . 我想一个字节数组的文字字符中String[]所以说,在第二个值String[]0F I want the first byte in Byte[] to be 0F and not 16 我希望Byte[]的第一个字节为0F而不是16

Any ideas? 有任何想法吗?

Edit2 EDIT2

Let me clarify. 让我澄清一下。 I don't want to convert my String[] values into Hexadecimal , as they are already Hexadecimal . 我不想将我的String[]值转换为十六进制 ,因为它们已经是十六进制了 I want to directly copy them to a Byte[] 我想直接将它们复制到Byte []

The problem is my string of values "10" "0F" "3E" 42" already has the hexadecimal value I want. I want the byte array to contain those exact values and not convert them, they are already hexadecimal form. 问题是我的值字符串"10" "0F" "3E" 42"已经具有我想要的十六进制值。我希望字节数组包含那些确切的值并且不转换它们,它们已经是十六进制格式。

You have to convert (or parse ) string in order to get byte since string and byte are different types : 您必须转换 (或解析string以获取byte因为stringbyte不同的类型

// 10 == 10d 
byte b = Convert.ToByte("10");     // if "10" is a decimal representation
// 16 == 0x10
byte b = Convert.ToByte("10", 16); // if "10" is a hexadecimal representation

If you want to process an array , you can try a simple Linq : 如果要处理数组 ,可以尝试使用简单的Linq

using System.Linq;

...

string[] hexValues = new string[] {
  "10", "0F", "3E", "42"};

byte[] result = hexValues
  .Select(value => Convert.ToByte(value, 16))
  .ToArray();

If you want to print out result as hexadecimal , use formatting ( "X2" format string - at least 2 hexadecimal digits, use captital letters): 如果要以十六进制格式输出result ,请使用格式设置( "X2"格式字符串-至少2 十六进制数字,请使用大写字母):

// 10, 0F, 3E, 42
Console.Write(string.Join(", ", result.Select(b => b.ToString("X2")))); 

Compare with same array but in a different format ( "d2" - at least 2 decimal digits) 相同数组比较,但格式不同"d2" -至少2 十进制数字)

// 16, 15, 62, 66 
Console.Write(string.Join(", ", result.Select(b => b.ToString("d2")))); 

If no format provided, .Net uses default one and represents byte in decimal: 如果未提供任何格式,.Net将使用默认格式,并以十进制表示byte

// 16, 15, 62, 66 
Console.Write(string.Join(", ", result));

You're really confusing representation and numbers here. 您在这里确实混淆了表示形式数字

A string like "0F" can be seen as a representation of a number in base 16, that is, in decimal representation, 16 . 像字符串"0F"可以被看作是在底座16一的数的表示,即,在十进制表示,16。

Which is the exact same thing as representing 16 as F or 0F or XVI or IIIIIIIIIIIIIIII or whatever other representation you choose. 与将16表示为F0FXVIIIIIIIIIIIIIIIII或您选择的任何其他表示形式完全相同。

The string "0F" actually looks in memory like this 字符串"0F"实际上在内存中看起来像这样

Hexadecimal representation: 十六进制表示:

0x30 0x46 0x00 

Decimal representation: 小数表示:

48 70 0

Binary representation: 二进制表示形式:

0b00110000 0b01000110 0b00000000

Byte is simply a data type which is infact a subset of an integer . Byte只是一种data type ,实际上是integer的子集。

Byte takes interger values ranging from -2^7(-128) to 2^7-1$(127) Byteinterger数值,范围从-2^7(-128) to 2^7-1$(127)

Calling Convert.ToByte(string, 16) simply converts your string to an equivalent hex value and then to an equivalent value in byte . 调用Convert.ToByte(string, 16)只需将string转换为等效的hex value ,然后转换为byte的等效值。

Note the byte data type is always an integer data but used in place of an integer just to save space in memory. 请注意, byte data type始终是integer data但用于代替整数只是为了节省内存空间。 As referenced above the byte datatype takes values from -128 to 127 thereby saving you more space in memory than the integer data type would. 如上所述,字节数据类型的值从-128 to 127从而比integer data type节省了更多的内存空间。

Please Note that you are likely to run into an error if the hexadecimal value you wish to convert to byte is less than -128 or greater than 127 请注意,如果要转换为bytehexadecimal值小于-128或大于127 ,则可能会出错。

The link below shows an instance of this error when I try converting a string whose value when converted to hexadecimal is greater than 127 . 当我尝试转换string (转换为hexadecimal时的值大于127时,下面的链接显示此错误的实例。

Error when converting to Byte 转换为字节时出错

You get an error whenever you do this. 每当您这样做时,您都会得到一个错误。

I hope my answer and Dmitry Bychenko's sheds more light into your problem. 希望我的回答和德米特里·拜琴科(Dmitry Bychenko)能够为您的问题提供更多的启示。 Please feel free to comment if it doesnt. 如果没有,请随时发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM