Android Byte Array to Unicode Convert bcdToString(byte[] data, int offset, int length)

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

Description

Many fields in GSM SIM's are stored as nibble-swizzled BCD Assumes left-justified field that may be padded right with 0xf values.

License

Apache License

Declaration

public static String bcdToString(byte[] data, int offset, int length) 

Method Source Code

//package com.java2s;
/*/* w  w w .j  a  v  a2  s  .  c o m*/
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed 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 {
    /**
     * Many fields in GSM SIM's are stored as nibble-swizzled BCD
     *
     * Assumes left-justified field that may be padded right with 0xf
     * values.
     *
     * Stops on invalid BCD value, returning string so far
     */
    public static String bcdToString(byte[] data, int offset, int length) {
        StringBuilder ret = new StringBuilder(length * 2);

        for (int i = offset; i < offset + length; i++) {
            byte b;
            int v;

            v = data[i] & 0xf;
            if (v > 9)
                break;
            ret.append((char) ('0' + v));

            v = (data[i] >> 4) & 0xf;
            // Some PLMNs have 'f' as high nibble, ignore it
            if (v == 0xf)
                continue;
            if (v > 9)
                break;
            ret.append((char) ('0' + v));
        }

        return ret.toString();
    }
}

Related

  1. jisx0208ToString(byte[] b, int offset, int len)
  2. toBase58(byte[] b)
  3. ushort2bytesBE(int val, byte[] b, int off)
  4. ushort2bytesLE(int val, byte[] b, int off)
  5. convertLongVowel(byte[] b)
  6. upperToLower(byte[] b)
  7. upperToLowerLatin(byte[] b)
  8. tag2bytesBE(int tag, byte[] b, int off)
  9. tag2bytesLE(int tag, byte[] b, int off)