Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Convert from byte[] to int and int to byte[]
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00974405
Message ID:
00974482
Vues:
17
I will post the code I have to convert from Int64 to byte[4] and from byte[4] to Int64.
Feel free to comment on the code. I am still searching of a simpler way of doing this:
System.Int64 paraInt64 = 4294967295;
System.Byte[] myByte = new System.Byte[4];

myByte[0] = Convert.ToByte(paraInt64/(256*256*256));

if (paraInt64/(256*256) <= 255)
  myByte[1] = Convert.ToByte(paraInt64/(256*256));
else
  myByte[1] = Convert.ToByte((paraInt64/(256*256))%256);

if (paraInt64/(256) <= 255)
  myByte[2] = Convert.ToByte(paraInt64/256);
else
  myByte[2] = Convert.ToByte((paraInt64/256)%256);

myByte[3] = Convert.ToByte(paraInt64%256);

System.Int64 iValue; 
iValue = (Convert.ToInt64(myByte[0]) * (256*256*256)) + (Convert.ToInt64(myByte[1]) * (256*256)) + (Convert.ToInt64(myByte[2]) * 256) + (Convert.ToInt64(myByte[3]));
Einar

>Is there an easy way to convert from a byte array to integer? Is there an equaly easy way to convert from integer to a byte array?
>
>Currently what I do for a byte array of length 2:
>
>System.Int32 iValue = 42000;
>System.Byte[] bValue = new System.Byte[2];
>
>bValue[0] = Convert.ToByte(iValue/256);
>bValue[1] = Convert.ToByte(iValue%256);
>
>System.Int32 iRetVal;
>iRetVal = Convert.ToInt32(Convert.ToInt32(bValue[0]) * 256 + Convert.ToInt32(bValue[1]));
>
>
>Now this isn't a lot of work for byte[2] but it will get kinda messy for byte[4]. I have got it working for byte[4], but I was wondering if there was a simpler/built in (one-liner) way of doing this.
>
>Any help would be appreciated.
>
>Thanks,
>Einar
Semper ubi sub ubi.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform