convert uint to byte[]
-
Hi all, I have a byte[] to unit function, I would like to have a reverse function that convert unit to byte[], please help. thanks a lot.
public static uint ParseUint(byte\[\] buffer, int uintLengthInBytes, ref int offset) { uint value = 0; int i, j; for (i = offset + uintLengthInBytes - 1, j = 0; i >= offset; i--, j++) value |= (uint)(buffer\[i\] << (8 \* j)); offset += uintLengthInBytes; return value; }
Regards, unruledboy_at_gmail_dot_com http://www.xnlab.com
-
Hi all, I have a byte[] to unit function, I would like to have a reverse function that convert unit to byte[], please help. thanks a lot.
public static uint ParseUint(byte\[\] buffer, int uintLengthInBytes, ref int offset) { uint value = 0; int i, j; for (i = offset + uintLengthInBytes - 1, j = 0; i >= offset; i--, j++) value |= (uint)(buffer\[i\] << (8 \* j)); offset += uintLengthInBytes; return value; }
Regards, unruledboy_at_gmail_dot_com http://www.xnlab.com
-
hi, I know that method, the result is wrong. please take a closer look at the original code :doh:
Regards, unruledboy_at_gmail_dot_com http://www.xnlab.com
-
hi, I know that method, the result is wrong. please take a closer look at the original code :doh:
Regards, unruledboy_at_gmail_dot_com http://www.xnlab.com
-
sorry, my debugging mistake, you are right. thanks a lot.:rose:
Regards, unruledboy_at_gmail_dot_com http://www.xnlab.com
-
Byte order reversal? Use System.BitConverter and System.Net.IPAddress.NetworkToHostOrder/HostToNetworkOrder. One I did earlier:
/// <summary> /// Byte reverse the value and write to the buffer at ofs /// </summary> /// <param name="i">value to write</param> /// <param name="ofs">Write offset in the buffer</param> internal void WriteUInt32(UInt32 i, Int32 ofs, Byte\[\] buffer) { Int32 j = IPAddress.HostToNetworkOrder(unchecked((Int32)i)); Byte\[\] b = BitConverter.GetBytes(j); b.CopyTo(buffer, ofs); }
Alan.
-
Byte order reversal? Use System.BitConverter and System.Net.IPAddress.NetworkToHostOrder/HostToNetworkOrder. One I did earlier:
/// <summary> /// Byte reverse the value and write to the buffer at ofs /// </summary> /// <param name="i">value to write</param> /// <param name="ofs">Write offset in the buffer</param> internal void WriteUInt32(UInt32 i, Int32 ofs, Byte\[\] buffer) { Int32 j = IPAddress.HostToNetworkOrder(unchecked((Int32)i)); Byte\[\] b = BitConverter.GetBytes(j); b.CopyTo(buffer, ofs); }
Alan.
-
sorry, my debugging mistake, you are right. thanks a lot.:rose:
Regards, unruledboy_at_gmail_dot_com http://www.xnlab.com