Android String to Byte Array Convert Str2Bcd(String asc)

Here you can find the source of Str2Bcd(String asc)

Description

Str Bcd

Parameter

Parameter Description
asc a parameter

Declaration

public static byte[] Str2Bcd(String asc) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from w ww.  j a  v a  2  s  .  com
     *
     * @param asc
     * @return
     */
    public static byte[] Str2Bcd(String asc) {
        int len = asc.length();
        int mod = len % 2;

        if (mod != 0) {
            asc = "0" + asc;
            len = asc.length();
        }

        byte abt[] = new byte[len];
        if (len >= 2) {
            len = len / 2;
        }

        byte bbt[] = new byte[len];
        abt = asc.getBytes();
        int j, k;

        for (int p = 0; p < asc.length() / 2; p++) {
            if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
                j = abt[2 * p] - '0';
            } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
                j = abt[2 * p] - 'a' + 0x0a;
            } else {
                j = abt[2 * p] - 'A' + 0x0a;
            }

            if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
                k = abt[2 * p + 1] - '0';
            } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
                k = abt[2 * p + 1] - 'a' + 0x0a;
            } else {
                k = abt[2 * p + 1] - 'A' + 0x0a;
            }

            int a = (j << 4) + k;
            byte b = (byte) a;
            bbt[p] = b;
        }

        return bbt;
    }
}

Related

  1. getBytesFromAddress(String address)
  2. stringToBytes(String string)
  3. String2Byte(String hexString)
  4. StringToByteArray(String input)
  5. StrToBcdBytes(String s)
  6. toBytes(String digits, int radix)
  7. toBytes(String hexString)
  8. toBytes(String hexString)
  9. toBytes(String s)