package seng302.Networking.Utils; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Arrays; /** * Created by fwy13 on 25/04/17. */ public class ByteConverter { public static int IntegerSize = 4; public static int LongSize = 8; public static int CharSize = 2; public static int ShortSize = 2; //default for AC35 is Little Endian therefore all overloads will be done with Little_Endian unless told else wise public static int bytesToInt(byte bite){ byte[] bytes = {bite}; return bytesToInt(bytes, ByteOrder.LITTLE_ENDIAN); } public static int bytesToInt(byte[] bytes){ return bytesToInt(bytes, ByteOrder.LITTLE_ENDIAN); } public static int bytesToInt(byte[] bytes, ByteOrder byteOrder){ byte[] bites = new byte[4]; if (byteOrder == ByteOrder.LITTLE_ENDIAN){ for (int i = 0; i < bytes.length; i++){ bites[i] = bytes[i]; if (i > 4){//break if over the limit break; } } for (int i = bytes.length; i < 4; i++){ bites[i] = 0b0; } }else{//if big endian for (int i = 0; i < 4 - bytes.length; i++) { bites[i] = 0b0; } for (int i = 4 - bytes.length; i < 4; i++) { bites[i] = bytes[i]; if (i > 4){//break if over the limit break; } } } return ByteBuffer.wrap(bites).order(byteOrder).getInt(); } public static long bytesToLong(byte bite){ byte[] bytes = {bite}; return bytesToLong(bytes, ByteOrder.LITTLE_ENDIAN); } public static long bytesToLong(byte[] bytes){ return bytesToLong(bytes, ByteOrder.LITTLE_ENDIAN); } public static long bytesToLong(byte[] bytes, ByteOrder byteOrder){ byte[] bites = new byte[8]; if (byteOrder == ByteOrder.LITTLE_ENDIAN) { for (int i = 0; i < 8 - bytes.length; i++) { bites[i] = 0b0; } for (int i = 8 - bytes.length; i < 8; i++) { bites[i] = bytes[i]; if (i > 8){//break if over the limit break; } } }else{//if big endian for (int i = 0; i < bytes.length; i++) { bites[i] = bytes[i]; if (i > 8){//break if over hte limit break; } } for (int i = bytes.length; i < 8; i++) { bites[i] = 0b0; } } return ByteBuffer.wrap(bites).order(byteOrder).getLong(); } public static short bytesToShort(byte bite){ byte[] bytes = {bite}; return bytesToShort(bytes, ByteOrder.LITTLE_ENDIAN); } public static short bytesToShort(byte bite, ByteOrder byteOrder){ byte[] bytes = {bite}; return bytesToShort(bytes, byteOrder); } public static short bytesToShort(byte[] bytes){ return bytesToShort(bytes, ByteOrder.LITTLE_ENDIAN); } public static short bytesToShort(byte[] bytes, ByteOrder byteOrder){ byte[] bites = new byte[2]; if (byteOrder == ByteOrder.LITTLE_ENDIAN) { for (int i = 0; i < bytes.length; i++) { bites[i] = bytes[i]; if (i > 2){//break if over hte limit break; } } for (int i = bytes.length; i < 2; i++) { bites[i] = 0b0; } }else{//if big endian for (int i = 0; i < 2 - bytes.length; i++) { bites[i] = 0b0; } for (int i = 8 - bytes.length; i < 2; i++) { bites[i] = bytes[i]; if (i > 2){//break if over the limit break; } } } return ByteBuffer.wrap(bites).order(byteOrder).getShort(); } public static byte[] intToBytes(int i){ return intToBytes(i, 4, ByteOrder.LITTLE_ENDIAN); } public static byte[] intToBytes(int i ,int size){ return intToBytes(i, size, ByteOrder.LITTLE_ENDIAN); } public static byte[] intToBytes(int i ,int size, ByteOrder byteOrder){ ByteBuffer buffer = ByteBuffer.allocate(IntegerSize); buffer.order(byteOrder); buffer.putInt(i); byte[] copy = buffer.array(); byte[] bytes = new byte[size]; if (byteOrder == ByteOrder.LITTLE_ENDIAN){ bytes = Arrays.copyOfRange(copy, 0, size); }else{// if it is Big Endian bytes = Arrays.copyOfRange(copy, IntegerSize - size, IntegerSize); } return bytes; } }