Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Andreas Fröwis afroewis

🎯
Focusing
View GitHub Profile
@afroewis
afroewis / bcd.cs
Last active Nov 8, 2021
C#: DateTime to BCD (Binary Coded Decimal)
View bcd.cs
public static byte[] FromDateTime(DateTime dateTime)
{
var daysFirstByte = dateTime.Day / 10;
var daysSecondByte = dateTime.Day - daysFirstByte * 10;
byte daysBcd = (byte)((daysFirstByte << 4) | daysSecondByte);
var monthsBcdFirstByte = dateTime.Month / 10;
var monthsBcdSecondByte = dateTime.Month - monthsBcdFirstByte * 10;
byte monthsBcd = (byte)((monthsBcdFirstByte << 4) | monthsBcdSecondByte);