Search code examples
javadelphibcd

Conversion byte decimal to byte bcd


I need to convert a Delphi function to Java function. This function convert a byte decimal to byte bcd:

function ByteToBCD(Number : byte) : byte;
begin
    result:= ((Number div 10) shl 4) or (Number mod 10);
end;

Solution

  • You can do this

    public static int byteToBCD(byte b) {
        assert 0 <= b && b <= 99; // two digits only.
        return (b / 10 << 4) | b % 10;
    }
    

    It is not clear what you are stuck on but it the answer is trivia.