简体   繁体   English

如何在C#中将具有十六进制内容的字节数组转换为具有十进制的字符串

[英]How to convert a byte array with hexadecimal contents to string with decimal in c#

Language : C# 语言:C#

Basically i have a Byte array which contains hexadecimal contents. 基本上我有一个包含十六进制内容的字节数组。

I want to convert it into a String and the hexadecimal contents should also be converted into Decimal contents. 我想将其转换为字符串,十六进制内容也应转换为十进制内容。

My final string should contain Equivalent Decimal Values of the Hexa Decimal values contained in the initial Byte Array. 我的最终字符串应包含初始字节数组中包含的十六进制十进制值的等效十进制值。

I converted byte array to string using System.Text.Encoding.GetEncoding(1251).GetString 我使用System.Text.Encoding.GetEncoding(1251).GetString将字节数组转换为字符串

But how to convert the Hex to Decimal ? 但是如何将十六进制转换为十进制? Even if we can do it in multiple steps it is not a problem. 即使我们可以分多个步骤进行操作,也不是问题。

sorry to ask silly doubts , please Spare. 抱歉提出愚蠢的疑问,请保留。

Thanks in Advance! 提前致谢!

It's not entirely clear what you mean. 您的意思还不清楚。

Byte arrays just contain byte values - they're just numbers. 字节数组仅包含字节值-它们只是数字。 In other words: 换一种说法:

byte x = 0x20;
byte y = 32;

are exactly the same - they both just set the value to be 32. 完全相同-它们都将值设为32。

Now, if you want to convert a byte array into a number, look at BitConverter and its methods like BitConverter.ToInt32 . 现在,如果要将字节数组转换为数字,请查看BitConverter及其类似BitConverter.ToInt32方法。 That will convert the byte array into a number (an int in that particular case) - you can then just call ToString() on the number to get a decimal representation as a string. 这会将字节数组转换为数字(在特定情况下为int )-然后,您可以仅在数字上调用ToString()即可获取小数形式的字符串。

How many bytes is your original data? 您的原始数据有多少个字节? That will be a key factor in determining which BitConverter method to call. 这将是确定要调用哪个BitConverter方法的关键因素。 You will also need to know the endianness of the data - if BitConverter in "normal" .NET is little endian; 您还需要知道数据的字节序-如果“普通” .NET中的BitConverter为小字节序; you might be interested in the EndianBitConverter class in my MiscUtil library if your data is really big-endian. 如果您的数据确实是big-endian,那么您可能对我的MiscUtil库中的EndianBitConverter类感兴趣。

I'm a little unsure what you're trying to do. 我有点不确定您要做什么。 Are you trying to create a comma separated string of the decimal numbers - eg: 您是否要创建一个用逗号分隔的十进制数字字符串-例如:

byte[] hexValues = { 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF };
string decimalValuesAsStringList = string.Join(",", hexValues.Select(i => i.ToString()).ToArray());

...or like this: ...或像这样:

char[] hexCharacters = { '9', 'A', 'B', 'C', 'D', 'E', 'F' };
byte[] hexValues = hexCharacters.Select(c => Convert.ToByte(c)).ToArray();
string decimalValuesAsStringList = string.Join(",", hexValues.Select(i => Convert.ToInt32(((char)i).ToString(), 16).ToString()).ToArray());

If your array really contains the character codes of the hexadecimal representation of numbers, then you don't have to bother with decoding. 如果您的数组确实包含数字的十六进制表示形式的字符代码,那么您就不必费心解码。 As the character codes for those characters are the same in all regular encodings, you can just cast the bytes to characters. 由于这些字符的字符代码在所有常规编码中都是相同的,因此您只需将字节转换为字符即可。

I wrote an extension that parses a stream of characters into a stream of bytes: 我写了一个扩展程序,将字符流解析为字节流:

static class Hex {

    public static IEnumerable<byte> ParseHex(this IEnumerable<char> chars) {
        int buffer = 0;
        bool first = true;
        foreach (char c in chars) {
            int b = (c - '0') % 32;
            if (b > 9) b -= 7;
            if (first) {
                buffer |= b << 4;
            } else {
                yield return (byte)(buffer | b);
                buffer = 0;
            }
            first = !first;
        }
        if (!first) {
                yield return (byte)buffer;
        }
    }

}

(If someone recognises part of the code, it's based on my code in this answer.) (如果有人识别出部分代码,则基于我在答案中的代码。)

Usage: 用法:

byte[] data = { 48, 70, 51, 67, 70, 56, 48, 55, 57, 49 };

string result = string.Join(",",
    data.Cast<char>().ParseHex().Select(b => b.ToString()).ToArray()
);

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

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