Android Byte Array to ASCII Convert bcdToAscii(byte[] bcdByte, int offset, int length)

Here you can find the source of bcdToAscii(byte[] bcdByte, int offset, int length)

Description

bcd To Ascii

Declaration

public static byte[] bcdToAscii(byte[] bcdByte, int offset, int length) 

Method Source Code

//package com.java2s;

public class Main {
    public static byte[] bcdToAscii(byte[] bcdByte) {
        byte[] returnByte = new byte[bcdByte.length * 2];
        byte value;
        for (int i = 0; i < bcdByte.length; i++) {
            value = (byte) (bcdByte[i] >> 4 & 0xF);
            if (value > 9) {
                returnByte[i * 2] = (byte) (value + (byte) 0x37);
            } else {
                returnByte[i * 2] = (byte) (value + (byte) 0x30);
            }/*w w  w.  j  av a2s .  c  o  m*/
            value = (byte) (bcdByte[i] & 0xF);
            if (value > 9) {
                returnByte[i * 2 + 1] = (byte) (value + (byte) 0x37);
            } else {
                returnByte[i * 2 + 1] = (byte) (value + (byte) 0x30);
            }
        }
        return returnByte;
    }

    public static byte[] bcdToAscii(byte[] bcdByte, int offset, int length) {
        byte[] returnByte = new byte[length * 2];
        byte value;
        for (int i = offset; i < length; i++) {
            value = (byte) (bcdByte[i] >> 4 & 0xF);
            if (value > 9) {
                returnByte[i * 2] = (byte) (value + (byte) 0x37);
            } else {
                returnByte[i * 2] = (byte) (value + (byte) 0x30);
            }
            value = (byte) (bcdByte[i] & 0xF);
            if (value > 9) {
                returnByte[i * 2 + 1] = (byte) (value + (byte) 0x37);
            } else {
                returnByte[i * 2 + 1] = (byte) (value + (byte) 0x30);
            }
        }
        return returnByte;
    }

    public static void bcdToAscii(byte[] bcd_buf, int offset,
            byte[] ascii_buf, int asc_offset, int conv_len, int type) {
        int cnt;
        int bcdOffset = offset;
        int asciiOffset = asc_offset;
        if (conv_len > (bcd_buf.length * 2)) {
            conv_len = (bcd_buf.length * 2);
        }
        if (((conv_len & 0x01) > 0) && (type > 0)) {
            cnt = 1;
            conv_len++;
        } else {
            cnt = 0;
        }
        for (; cnt < conv_len; cnt++, asciiOffset++) {
            ascii_buf[asciiOffset] = (byte) (((cnt & 0x01) > 0) ? (bcd_buf[bcdOffset++] & 0x0f)
                    : ((bcd_buf[bcdOffset] & 0xFF) >>> 4));
            ascii_buf[asciiOffset] += (byte) ((ascii_buf[asciiOffset] > 9) ? (65 - 10)
                    : 48); // 65 = 'A' 48 = '0'
        }
    }
}

Related

  1. toAsciiChars(byte[] raw)
  2. Bcd2Ascii(byte[] bcd)
  3. getBCD2(byte[] b, int offset)
  4. getBCD4(byte[] b, int offset)
  5. bcdToAscii(byte[] bcdByte)
  6. bcdToAscii(byte[] bcd_buf, int offset, byte[] ascii_buf, int asc_offset, int conv_len, int type)
  7. toAsciiString(byte[] data)
  8. toAsciiString(byte[] output)