3

I have a String[] of hex values "10" "0F" "3E" "42" stored.

I found this method to convert to a 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".

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. I want the first byte in Byte[] to be 0F and not 16

Any ideas?

Edit2

Let me clarify. I don't want to convert my String[] values into Hexadecimal, as they are already Hexadecimal. I want to directly copy them to a 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.

8
  • byte[] result hexValues.Select(value => Convert.ToByte(value, 16)).ToArray(); Commented Oct 2, 2018 at 19:49
  • 1
    What is your desired output? I think you are confused because you are viewing the values as base 10. Commented Oct 2, 2018 at 19:52
  • I want a byte array with the literal characters in the String[] So say the second value in String[] is 0F. I want the first in Byte[] to be 0F and not 16. Commented Oct 2, 2018 at 19:55
  • What is HexStrings value? Commented Oct 2, 2018 at 19:56
  • 4
    byte doesn't have any format (decimal, hexadecimal, binary etc.) inside it. byte b = 0x0f; is neither hexadecimal nor decimal, it's just a value. It can be represented as decimal: b.ToString() or hexadecimal b.ToString("x2") Commented Oct 2, 2018 at 20:14

3 Answers 3

6

You have to convert (or parse) string in order to get byte since string and byte are different types:

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

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

// 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)

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

// 16, 15, 62, 66 
Console.Write(string.Join(", ", result));
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Which is the exact same thing as representing 16 as F or 0F or XVI or IIIIIIIIIIIIIIII or whatever other representation you choose.

The string "0F" actually looks in memory like this

Hexadecimal representation:

0x30 0x46 0x00 

Decimal representation:

48 70 0

Binary representation:

0b00110000 0b01000110 0b00000000

1 Comment

The problem with my understanding is that my string - "10" "0F" "3E" "42" Really is 0x10, 0x0F, 0x3E, 0x42. I didnt want any conversion, it was already Hex. My confusion came because the byte array list was using the decimal value (16 for 0x10) for example and I didn't realize that bytes can be stored as decimal. I was expecting 0x10 in the byte array and not 16.
0

Byte is simply a data type which is infact a subset of an integer.

Byte takes interger values ranging from -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.

Note the byte data type is always an integer data but used in place of an integer just to save space in memory. 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.

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

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.

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. Please feel free to comment if it doesnt.

Comments

Your Answer

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