Java BCD Convert To bcdToString(byte[] bcd)

Here you can find the source of bcdToString(byte[] bcd)

Description

bcd To String

License

Apache License

Declaration

public static String bcdToString(byte[] bcd) 

Method Source Code

//package com.java2s;
/*/* w ww. j a va2s  . co  m*/
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */

public class Main {
    private static final char s_ucEndMask[] = { 0xf0, 0xf };
    private static final char s_ucBitOffset[] = { 4, 0 };

    public static String bcdToString(byte[] bcd) {
        byte bcd_value;
        int i;
        char[] str = new char[bcd.length * 2];
        for (i = 0; i < bcd.length * 2; i++) {
            if ((bcd[i / 2] & s_ucEndMask[i % 2]) == s_ucEndMask[i % 2]) {
                break;
            }

            if (i >= bcd.length * 2) {
                break;
            }

            bcd_value = (byte) ((bcd[i / 2] >> s_ucBitOffset[i % 2]) & 0xf);

            switch (bcd_value) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
                str[i] = (char) (bcd_value + '0');
                break;

            case 0x0A:
                str[i] = 'A';
                break;

            case 0x0B:
                str[i] = '*';
                break;

            case 0x0C:
                str[i] = '#';
                break;

            case 0x0D:
                str[i] = 'D';
                break;

            case 0x0E:
                str[i] = 'E';
                break;

            case 0x0F:
            default:
                str[i] = 'F';
                break;
            }
        }

        StringBuilder stb = new StringBuilder("");
        for (int k = 0; k < i; k++) {
            stb.append(str[k]);
        }
        return stb.toString();
    }
}

Related

  1. bcdNibbleToInt(byte b, boolean high)
  2. bcdToByte(byte src)
  3. BCDtoInt(int bcd)
  4. BCDToLong(byte[] bcdByte)
  5. bcdToLong(byte[] src, int flag)