Conversione di un int in una matrice di byte BCD
-
20-09-2019 - |
Domanda
voglio convertire un int a un byte [2] array utilizzando BCD.
L'int in questione verrà dalla DateTime che rappresenta l'anno e deve essere convertito in due byte.
C'è qualche funzione di pre-made che fa questo o potete darmi un modo semplice di fare questo?
Esempio:
int year = 2010
uscita sarebbe:
byte[2]{0x20, 0x10};
Soluzione
static byte[] Year2Bcd(int year) {
if (year < 0 || year > 9999) throw new ArgumentException();
int bcd = 0;
for (int digit = 0; digit < 4; ++digit) {
int nibble = year % 10;
bcd |= nibble << (digit * 4);
year /= 10;
}
return new byte[] { (byte)((bcd >> 8) & 0xff), (byte)(bcd & 0xff) };
}
Attenzione che hai chiesto per un risultato big-endian, che è un po 'insolito.
Altri suggerimenti
Ecco una terribile versione di forza bruta. Sono sicuro che c'è un modo migliore di questo, ma dovrebbe funzionare comunque.
int digitOne = year / 1000;
int digitTwo = (year - digitOne * 1000) / 100;
int digitThree = (year - digitOne * 1000 - digitTwo * 100) / 10;
int digitFour = year - digitOne * 1000 - digitTwo * 100 - digitThree * 10;
byte[] bcdYear = new byte[] { digitOne << 4 | digitTwo, digitThree << 4 | digitFour };
La parte triste a questo proposito è che veloce binario a BCD conversioni sono costruiti con l'architettura x86 microprocessore, se si potesse arrivare a loro!
Ecco una versione leggermente più pulito quindi Jeffrey di
static byte[] IntToBCD(int input)
{
if (input > 9999 || input < 0)
throw new ArgumentOutOfRangeException("input");
int thousands = input / 1000;
int hundreds = (input -= thousands * 1000) / 100;
int tens = (input -= hundreds * 100) / 10;
int ones = (input -= tens * 10);
byte[] bcd = new byte[] {
(byte)(thousands << 4 | hundreds),
(byte)(tens << 4 | ones)
};
return bcd;
}
soluzione più comune
private IEnumerable<Byte> GetBytes(Decimal value)
{
Byte currentByte = 0;
Boolean odd = true;
while (value > 0)
{
if (odd)
currentByte = 0;
Decimal rest = value % 10;
value = (value-rest)/10;
currentByte |= (Byte)(odd ? (Byte)rest : (Byte)((Byte)rest << 4));
if(!odd)
yield return currentByte;
odd = !odd;
}
if(!odd)
yield return currentByte;
}
Ho fatto una routine generica pubblicato su IntToByteArray che si potrebbe usare come:
var = yearInBytes ConvertBigIntToBcd (2010, 2);
static byte[] IntToBCD(int input) {
byte[] bcd = new byte[] {
(byte)(input>> 8),
(byte)(input& 0x00FF)
};
return bcd;
}