Skip to content
Snippets Groups Projects
Commit 94621bc0 authored by jkerdreu's avatar jkerdreu
Browse files

Split package



git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Java/branches/0.7@2622 b32b6428-25c9-4566-ad07-03861ab6144f
parent 5b397adb
Branches
Tags
No related merge requests found
package imt.xaal.lib;
public class BinTools {
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
/**
* Transform a byte array into a it's hexadecimal representation
*/
public static String hexlify(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
String ret = new String(hexChars);
return ret;
}
/**
* Transform a string of hexadecimal chars into a byte array
*/
public static byte[] unhexlify(String argbuf) {
int arglen = argbuf.length();
if (arglen % 2 != 0)
throw new RuntimeException("Odd-length string");
byte[] retbuf = new byte[arglen/2];
for (int i = 0; i < arglen; i += 2) {
int top = Character.digit(argbuf.charAt(i), 16);
int bot = Character.digit(argbuf.charAt(i+1), 16);
if (top == -1 || bot == -1)
throw new RuntimeException("Non-hexadecimal digit found");
retbuf[i / 2] = (byte) ((top << 4) + bot);
}
return retbuf;
}
public static byte[] longToByteArray(long value, int size) {
byte[] result = new byte[size];
for (int i = size-1; i>=0; i--) {
byte r = (byte) (value >>> (i * 8));
result[(size-1)-i] = r;
}
return result;
}
}
\ No newline at end of file
package imt.xaal.lib;
import java.nio.ByteBuffer;
import java.util.UUID;
public class UUIDUtils {
public static byte[] getBytesFromUUID(UUID uuid) {
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
return bb.array();
}
public static UUID getUUIDFromBytes(byte[] bytes) {
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
Long high = byteBuffer.getLong();
Long low = byteBuffer.getLong();
return new UUID(high, low);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment