0

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!

1
  • Can you give examples - does converting to decimal mean { 0x1, 0x0, 0x3 } become the string "1,0,3" or "259" or "719"? Commented Mar 28, 2009 at 15:57

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.