
com.github.housepower.jdbc.misc.BytesUtil Maven / Gradle / Ivy
/*
* 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.github.housepower.jdbc.misc;
public class BytesUtil {
public static byte[] longToBytes(long l) {
byte[] result = new byte[Long.BYTES];
for (int i = Long.BYTES - 1; i >= 0; i--) {
result[i] = (byte) (l & 0xFF);
l >>= Byte.SIZE;
}
return result;
}
public static byte[] longsToBytes(final long []longArray) {
byte[] result = new byte[Long.BYTES * longArray.length];
for (int index = 0; index < longArray.length; index++) {
long l = longArray[index];
for (int i = Long.BYTES - 1; i >= 0; i--) {
result[index * Long.BYTES + i] = (byte) (l & 0xFF);
l >>= Byte.SIZE;
}
}
return result;
}
public static long bytesToLong(final byte[] b) {
long result = 0;
for (int i = 0; i < Long.BYTES; i++) {
result <<= Byte.SIZE;
result |= (b[i] & 0xFF);
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy