Skip to content

Instantly share code, notes, and snippets.

@afroewis
afroewis / bcd.cs
Last active November 8, 2021 14:05
C#: DateTime to BCD (Binary Coded Decimal)
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);