Reputation:
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.
Upvotes: 2
Views: 16094
Reputation: 121
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
.
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.
Upvotes: 0
Reputation: 13652
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
Upvotes: 0
Reputation: 186668
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));
Upvotes: 6