Here you can find the source of intFromSyncSafeByteArray(byte[] buf, int offset, int len)
Parameter | Description |
---|---|
buf | byte array source |
offset | into the array |
len | number of bytes to use, must be 4 or less |
public static int intFromSyncSafeByteArray(byte[] buf, int offset, int len)
//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; } }