Java Integer From intFromSyncSafeByteArray(byte[] buf, int offset, int len)

Here you can find the source of intFromSyncSafeByteArray(byte[] buf, int offset, int len)

Description

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

License

Open Source License

Parameter

Parameter Description
buf byte array source
offset into the array
len number of bytes to use, must be 4 or less

Return

an int

Declaration

public static int intFromSyncSafeByteArray(byte[] buf, int offset, int len) 

Method Source Code

//package com.java2s;
/**/*from  ww w .  j  a  v  a  2  s.co 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.
 */

public class Main {
    /** 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

  1. intFromHex(String hex, boolean signed)
  2. intFromJSON(String[] json, int pos)
  3. intFromLex(byte[] bytes)
  4. IntFromRGB(int r, int g, int b)
  5. intFromString(String str, int dflt)
  6. intFromUnsignedByte(byte b)