utility to generate an int from an array of bytes in sync safe format. - Java java.lang

Java examples for java.lang:byte Array to int

Description

utility to generate an int from an array of bytes in sync safe format.

Demo Code

/**//from   ww  w .  jav  a 2  s.  c  o m
 * static byte array utilities
 *
 * Copyright (c) 2004, Pat Farrell, All rights reserved.
 * 
 * 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. * 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.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] buf = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        int offset = 2;
        int len = 2;
        System.out.println(intFromSyncSafeByteArray(buf, offset, len));
    }

    /** utility to generate an int from an array of bytes in syncsafe format. 
     * This format uses only 7 bits per byte
     * @param buf byte array source
     * @param offset into the array
     * @param len number of bytes to use, must be 4 or less
     * @return an int
     */
    public static int intFromSyncSafeByteArray(byte[] buf, int offset,
            int len) {
        int val = 0;
        int v2 = 0;
        for (int i = 0; i < len; i++) {
            val = val << 7;
            v2 = v2 << 7;
            int d = (char) (0x0000007F & buf[i + offset]); // use only low bits
            int d2 = buf[i + offset];
            if (d2 < 0)
                d2 = 256 + d2;
            val += d;
            v2 += d2;
        }
        if (val != v2)
            System.out.println(val + " != " + v2);
        if (val > 64000) {
            System.err.println("Val is way too big: " + val);
            System.err.println("off: " + offset + " and len: " + len);
            for (int i = 0; i < len; i++) {
                byte b = buf[i];
                System.err.println(i + "]=" + Integer.toHexString(b)
                        + " as char "
                        + (new Character((char) b).toString()));
            }
        }
        return val;
    }
}

Related Tutorials