Hi
Please see my comments next to each coded expression and correct where necessary.
// Declare a static method which returns an array of bytes and accepts 1 input argument named hex of type string.
private static byte[] HexToBytes(string hex)
// Construct a new instance\object of type byte and pass the hex value and length property divided by two. Store the result in a local variable named bytes.
byte[] bytes = new byte[hex.Length / 2];
// Initialize i to 0, assign the hex Length property to be less than i / 2 and then iterate the loop with i++
for (int i = 0; i < hex.Length / 2; i++)
// Use the Substring method of string passing in the values i times 2 and 2 and then store the return value in a local variable named code.
string code = hex.Substring(i * 2, 2);
// Pass the code reference and the enum NumberStyles.HexNumber to the byte Parse method and then Set this to the reference bytes in position 0.
bytes[i] = byte.Parse(code, NumberStyles.HexNumber);
// Return the bytes reference.
return bytes;
Thanks
Loading
VulpesPosted Mar 11, 2012, 2:54 PM
VulpesPosted Mar 14, 2012, 4:23 PM
Guest UserPosted Mar 14, 2012, 3:38 PM
Just out of curiousity your example slightly differs from my example and I just wondered how it still works. For example I have provided both examples:-
My example
byte[] bytes = new byte[hex.Length / 2];
for (int i = 0; i < hex.Length / 2; i++)
string code = hex.Substring(i * 2, 2);
bytes[i] = byte.Parse(code, NumberStyles.HexNumber);
return bytes;
Your example
string hex = "A0B97762";
for (int i = 0; i < hex.Length/2; i++)
string byteStr = hex.Substring(i * 2, 2);
byte b = byte.Parse(byteStr, NumberStyles.HexNumber);
Console.WriteLine("i = {0}, 2 * i = {1}, byteStr = {2}, byte = {3}", i, i * 2, byteStr, b);
Obviously at first glance, in your example the local variable hex has been declared along with the Console.Writeline which makes sense. However in mine there is a new byte array declared ie byte[] bytes = new byte[hex.Length / 2]; and also the byte.Parse method is Set to the bytes array at first position ie bytes[i] = byte.Parse(code, NumberStyles.HexNumber);
Could you explain why this has been left out of your example please?
Thanks!
VulpesPosted Mar 13, 2012, 2:58 PM
The next argument (2) tells the method how many characters to take from and including that index.
So, if you have this:
the output will be this:
Guest UserPosted Mar 13, 2012, 2:17 PM
Sorry to prolong this thread.
With the second argument of the Substring method being 2, how comes the method cannot work out that there will be 2 char pairs from the i * 2 statement? I dont see why 2 is needed, sorry for being dense here.
Thanks
Steven
VulpesPosted Mar 13, 2012, 12:28 PM
So the successive values of i * 2 would be 0, 2, 4 and 6.
There are 8 characters in the string with indices from 0 to 7, so this would pick out successive character pairs.
The first argument to the Substring method is the starting index (i * 2 here) and the second argument is the number of characters to take (2 here).
Guest UserPosted Mar 13, 2012, 12:26 PM
Another question if I may please...
Why would i * 2 iterate from 0 to 6 this would only make 7 chars not 8?
Also would it be the i * 2 that does the increments in 2 or the 2 length ie the 2nd passed in value?
Thanks
Guest UserPosted Mar 12, 2012, 9:20 AM
VulpesPosted Mar 12, 2012, 8:57 AM
If say the length of the string were 8, then as 'i' iterates from 0 to 3 in increments of 1 , then i * 2 will iterate from 0 to 6 in increments of 2 which is what we want here.
Guest UserPosted Mar 12, 2012, 8:29 AM
So what would be the point of putting any multiplication ie i * 2 if it will always be zero?
VulpesPosted Mar 12, 2012, 8:22 AM
So, if i is zero, then i * 2 is also zero because the result of multiplying any number by zero is always zero.
Guest UserPosted Mar 12, 2012, 8:20 AM
In your answer...
So, if i = 0, i * 2 is also 0 - how is this the case, surely i should still be 0 not i * 2 which would read i times 2?
VulpesPosted Mar 12, 2012, 7:56 AM
Guest UserPosted Mar 12, 2012, 7:46 AM
Could I get clarification on the following expression please
Given Substring(i * 2, 2) am I correct in saying that i * 2 will get the 2nd char in the string and then get a length of 2?
Thanks