1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
public static int ToInt(this byte[] This, int index = 0)
=> (int)(This.ToValue(index, 4) % int.MaxValue);
public static long ToValue(this byte[] This, int startPosition = 0, byte length = 2, )
{
if (length > 8)
throw new OverflowException("La longueur du tableau d'octets à convertir ne peut pas être supérieure à 8");
var bytes = This.SubBytes(startPosition, length);
return bytes.Select((o, index) => new { o, index })
.Aggregate((long)0, (agg, next) => agg + next.o << 8 * next.index);
}
public static byte[] SubBytes(this byte[] This, int startIndexPosition = 0, int? length = null)
{
// Récupérer la position de départ
var beginAt = This.ManageStartIndexPosition(startIndexPosition);
// Récupérer la taille à récupérer
var size = This.ManageLength(startIndexPosition, length);
// Nouveau tableau d'octets
var result = new byte[size];
// Ne récupérer que les données demandées
Buffer.BlockCopy(This, beginAt, result, 0, size);
// Retour du résultat de la fonction
return result;
}
public static int ManageStartIndexPosition(this byte[] This, int? startIndexPosition = null)
=> Math.Max(Math.Min(This.Length, startIndexPosition ?? This.Length), 0);
private static int ManageLength(this byte[] This, int startIndexPosition = 0, int? length = null)
=> Math.Max(Math.Min(Math.Min(This.Length, length ?? This.Length), This.Length - startIndexPosition), 0); |
Partager