Solution 1:
static string[] ToHexArray(uint value) {
int len = sizeof(uint);
string[] result = new string[len];
for (int index = 0; index < len; ++index) {
int shift = 8 * (len - index - 1);
uint mask = (uint)byte.MaxValue << shift;
uint masked = value & mask;
result[index] = ((byte)(masked >> shift)).ToString("X");
}
return result;
}
Solution 2:
static string[] ToHexArray(uint value) {
return System.Array.ConvertAll(
System.BitConverter.GetBytes(value),
new System.Converter<byte, string>(
(src) => {
return src.ToString("X");
}));
}
First solution puts most significant figure at left (you can easily change it in code), second one — at right (Assembly language style; you cannot change it in algorithm but can reverse it later). You can add "0x" prefix to each string by using
string.Format
instead of
ToString
. You can use:
string[] hexArray = ToHexArray(
uint.Parse(
MyTextBox.Text,
System.Globalization.NumberStyles.HexNumber));
Both solutions are tested.
Enjoy!
—SA