Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert whole byte[] into uint

Tags:

c#

.net

I have byte[] srno in my code

byte[] srno = new byte[6];

srno[0] = 0xff;
srno[1] = 0x0f;
srno[2] = 0x24;
srno[3] = 0x12;
srno[4] = 0x16;
srno[5] = 0x0a;

now I want this value in uint like

uint a = 0xff0f2412160a;

How to convert it?

like image 497
Kevan Avatar asked Feb 21 '13 12:02

Kevan


1 Answers

As @animaonline suggested, you should use BitConverter to convert byte array to uint or *ulong. Thus you have 6 bytes, uint is too small for you. You should convert to ulong*. But converter requires eight bytes, so create new array with required number of bytes:

byte[] value = new byte[8];
Array.Reverse(srno); // otherwise you will have a1612240fff result
Array.Copy(srno, value, 6);
ulong result = BitConverter.ToUInt64(value, 0);
Console.WriteLine("{0:x}", result); // ff0f2412160a
like image 155
Sergey Berezovskiy Avatar answered Oct 09 '22 08:10

Sergey Berezovskiy