Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public static byte[] convertIntToBCDBytes(int value) {
        int length = new String(value + "").length();
        if (length <= 2) {
            int first = value / 10;
            int second = value % 10;
            byte[] b = new byte[] { (byte) (first << 4 | second) };
            return b;
        } else if (length <= 4) {
            int first = (value / 1000) % 10;
            int second = (value / 100) % 10;
            int third = (value / 10) % 10;
            int fourth = value % 10;
            byte[] b = new byte[] { (byte) (first << 4 | second), (byte) (third << 4 | fourth) };
            return b;
        } else {
            int prefirst = value / 10000;
            int first = (value / 1000) % 10;
            int second = (value / 100) % 10;
            int third = (value / 10) % 10;
            int fourth = value % 10;
            byte[] b = new byte[] { (byte) prefirst, (byte) (first << 4 | second), (byte) (third << 4 | fourth) };
            return b;
        }
    }
}