ByteUtils.java 3.84 KB
/*
 * Copyright Codecrew Ry
 * 
 * All rights reserved.
 * 
 * This license applies to any software containing a notice placed by the 
 * copyright holder. Such software is herein referred to as the Software. 
 * This license covers modification, distribution and use of the Software. 
 * 
 * Any distribution and use in source and binary forms, with or without 
 * modification is not permitted without explicit written permission from the 
 * copyright owner. 
 * 
 * A non-exclusive royalty-free right is granted to the copyright owner of the 
 * Software to use, modify and distribute all modifications to the Software in 
 * future versions of the Software. 
 * 
 */
package fi.codecrew.moya.utilities;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * Copyright Iudex / Tuomas Riihimäki
 * 
 */
public class ByteUtils {

	public static int parseUnsigned(byte... b) {
		if (b.length > 4) {
			throw new RuntimeException("Integer should never have more than 4 bytes!");
		}
		return new BigInteger(1, b).intValue();
	}

	public static Long parseUnsignedLong(byte[] b) {
		if (b.length > 8) {
			throw new RuntimeException("Long should never have more than 4 bytes!");
		}
		return new BigInteger(1, b).longValue();
	}

	public static short parseSigned(byte b0, byte b1) {
		return (short) (((b0 & 0xff) << 8) | (b1 & 0xff));
	}

	public static Integer parseSignedO(byte b, byte c) {
		return Integer.valueOf(parseSigned(b, c));
	}

	public static Integer parseSigned(byte b1, byte b2, byte b3, byte b4) {
		return (b1 & 0xff) << 24 | (b2 & 0xff) << 16 | (b3 & 0xff) << 8 | (b4 & 0xff);
	}

	public static void main(String[] ads)
	{

		System.out.println(parseUnsigned((byte) 0x31, (byte) 0x79));
		// System.out.println(parseSignedDecimal("+5.123"));
		// System.out.println(parseSignedDecimal("-5.123"));
		// System.out.println(parseSignedDecimal(" - 5123"));
		// System.out.println(parseSignedDecimal(null));
		// System.out.println(parseSignedDecimal(""));
		// System.out.println(parseSignedDecimal("  "));
	}

	public static BigDecimal parseSignedDecimal(String number) {
		if (number == null || (number = number.trim()).isEmpty()) {
			return null;
		}
		number = number.trim();
		char firstChar = number.charAt(0);
		boolean signPositive = true;
		switch (firstChar) {
		case '-':
			signPositive = false;
		case '+':
			number = number.substring(1).trim();
		}

		BigDecimal ret = new BigDecimal(number);
		if (!signPositive) {
			ret = ret.negate();
		}
		return ret;
	}

	// public static int parseSignedInt(byte... b) {
	// if (b.length > 4) {
	// throw new
	// RuntimeException("Integer should never have more than 4 bytes!");
	// }
	// // return new BigInteger(1, b).intValue();
	//
	// int value = (b[b.length - 1] & 0xff);
	// if (b.length > 1) {
	// value |= (b[b.length - 2] & 0xff) << 8;
	// }
	// if (b.length > 2) {
	// value |= (b[b.length - 3] & 0xff) << 16;
	// }
	// if (b.length > 3) {
	// value |= (b[b.length - 4] & 0xff) << 24;
	// }
	// return value;
	// }
	/**
	 * Copies the integer value to four bytes in the destination bytearray beginning from the offset
	 * 
	 * Notice! This function handles signed integers: -1 -> 0xff 0xff 0xff 0xff
	 * 
	 * @param dst
	 * @param offset
	 * @param value
	 */
	public static void intToBytearray(byte[] dst, int offset, int value) {
		dst[offset++] = (byte) (value >>> 24);
		dst[offset++] = (byte) (value >>> 16);
		dst[offset++] = (byte) (value >>> 8);
		dst[offset] = (byte) (value);
	}

	public static byte[] toArray(int... bytes) {
		byte[] ret = new byte[bytes.length];
		for (int i = 0; i < bytes.length; ++i) {
			ret[i] = (byte) (bytes[i]);
		}
		return ret;
	}

	public static String toHexString(byte[] bytes) {
		StringBuilder sb = new StringBuilder();
		for (byte b : bytes) {
			int val = ((int) b) & 0xff;
			if (val < 0x10)
				sb.append("0");
			sb.append(Integer.toHexString(val));
		}
		return sb.toString();

	}

}