Existe-t-il un meilleur moyen d'écrire cela que d'utiliser BitConverter
?C#: conversion manuelle des bits?
public static class ByteArrayExtensions
{
public static int IntFromUInt24(this byte[] bytes)
{
if (bytes == null)
{
throw new ArgumentNullException();
}
if (bytes.Length != 3)
{
throw new ArgumentOutOfRangeException
("bytes", "Must have length of three.");
}
return BitConverter.ToInt32
(new byte[] { bytes[0], bytes[1], bytes[2], 0 }, 0);
}
}
Vous pouvez utiliser l'opérateur Bit Shift '<<' pour déplacer les valeurs des octets [0-2]. – davisoa
http://stackoverflow.com/questions/3559183/reading-an-unsigned-24-bit-integer-from-ac-stream –