Java ASCII ascii2BCD(byte[] val, int len)

Here you can find the source of ascii2BCD(byte[] val, int len)

Description

ascii BCD

License

Open Source License

Declaration

public static byte[] ascii2BCD(byte[] val, int len) 

Method Source Code

//package com.java2s;

public class Main {

    public static byte[] ascii2BCD(byte[] val, int len) {
        byte[] valByte = new byte[(len + 1) / 2];
        if (len % 2 == 0) {
            for (int i = 0; i < len; i++) {
                byte b = val[i];
                if (b > '9') {
                    b = (byte) (b % 0x10 + 9);
                } else {
                    b = (byte) (b % 0x10);
                }/*from   w  w  w. ja  v  a  2  s  . c om*/
                if (i % 2 == 0) {
                    valByte[i / 2] = (byte) (b * 0x10);
                } else {
                    valByte[i / 2] += b;
                }
            }
        } else {
            valByte[0] = (byte) (val[0] % 0x10);
            for (int i = 1; i < len; i++) {
                byte b = val[i];
                if (b > '9') {
                    b = (byte) (b % 0x10 + 9);
                } else {
                    b = (byte) (b % 0x10);
                }

                if (i % 2 != 0) {
                    valByte[(1 + i) / 2] = (byte) (b * 0x10);
                } else {
                    valByte[(1 + i) / 2] += b;
                }
            }
        }
        return valByte;
    }
}

Related

  1. ascii(final int c)
  2. ascii(String string)
  3. ascii2Bcd(byte[] asc)
  4. AsciiBTAddressToBytes(String btAddress)
  5. asciiBytes(String x)
  6. asciiChar(int value)
  7. asciiChars()