Vijay
Vijay

Reputation: 27

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

Language : 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

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!

Upvotes: 0

Views: 5733

Answers (3)

dommer
dommer

Reputation: 19810

I'm a little unsure what you're trying to do. Are you trying to create a comma separated string of the decimal numbers - e.g:

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());

Upvotes: 0

Guffa
Guffa

Reputation: 700232

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()
);

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500075

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.

Now, if you want to convert a byte array into a number, look at BitConverter and its methods like 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.

How many bytes is your original data? That will be a key factor in determining which BitConverter method to call. You will also need to know the endianness of the data - if BitConverter in "normal" .NET is little endian; you might be interested in the EndianBitConverter class in my MiscUtil library if your data is really big-endian.

Upvotes: 1

Related Questions