Fixed some issues

- Fixed issue where everything was inputted as an integer when converting to a byte array.
- Seperated loops that are used often to make it more readable.
#story[782]
main
Fan-Wu Yang 9 years ago
parent 413ab7b768
commit 8223ae46a1

@ -17,6 +17,14 @@ public class ByteConverter {
//default for AC35 is Little Endian therefore all overloads will be done with Little_Endian unless told else wise
//////////////////////////////////////////////////
//Bytes[] to number conversions
//////////////////////////////////////////////////
//////////////////////////////////////////////////
//Integer
//////////////////////////////////////////////////
public static int bytesToInt(byte bite){
byte[] bytes = {bite};
return bytesToInt(bytes, ByteOrder.LITTLE_ENDIAN);
@ -27,31 +35,14 @@ public class ByteConverter {
}
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;
}
}
}
byte[] bites = convertBytesToNum(bytes,byteOrder, IntegerSize);
return ByteBuffer.wrap(bites).order(byteOrder).getInt();
}
//////////////////////////////////////////////////
//Long
//////////////////////////////////////////////////
public static long bytesToLong(byte bite){
byte[] bytes = {bite};
return bytesToLong(bytes, ByteOrder.LITTLE_ENDIAN);
@ -62,71 +53,84 @@ public class ByteConverter {
}
public static long bytesToLong(byte[] bytes, ByteOrder byteOrder){
byte[] bites = new byte[8];
if (byteOrder == ByteOrder.LITTLE_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;
}
}else{//if big 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;
}
}
}
byte[] bites = convertBytesToNum(bytes,byteOrder, LongSize);
return ByteBuffer.wrap(bites).order(byteOrder).getLong();
}
//////////////////////////////////////////////////
//Short
//////////////////////////////////////////////////
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];
byte[] bites = convertBytesToNum(bytes,byteOrder, ShortSize);
return ByteBuffer.wrap(bites).order(byteOrder).getShort();
}
//////////////////////////////////////////////////
//Char
//////////////////////////////////////////////////
public static char bytesToChar(byte bite){
byte[] bytes = {bite};
return bytesToChar(bytes, ByteOrder.LITTLE_ENDIAN);
}
public static char bytesToChar(byte[] bytes){
return bytesToChar(bytes, ByteOrder.LITTLE_ENDIAN);
}
public static char bytesToChar(byte[] bytes, ByteOrder byteOrder){
byte[] bites = convertBytesToNum(bytes,byteOrder, CharSize);
return ByteBuffer.wrap(bites).order(byteOrder).getChar();
}
//////////////////////////////////////////////////
//Conversion Function
//////////////////////////////////////////////////
private static byte[] convertBytesToNum(byte[] bytes, ByteOrder byteOrder, int maxSize){
byte[] bites = new byte[maxSize];
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
if (i > maxSize){//break if over hte limit
break;
}
}
for (int i = bytes.length; i < 2; i++) {
for (int i = bytes.length; i < maxSize; i++) {
bites[i] = 0b0;
}
}else{//if big endian
for (int i = 0; i < 2 - bytes.length; i++) {
for (int i = 0; i < maxSize - bytes.length; i++) {
bites[i] = 0b0;
}
for (int i = 8 - bytes.length; i < 2; i++) {
for (int i = maxSize - bytes.length; i < maxSize; i++) {
bites[i] = bytes[i];
if (i > 2){//break if over the limit
if (i > maxSize){//break if over the limit
break;
}
}
}
return ByteBuffer.wrap(bites).order(byteOrder).getShort();
return bites;
}
//////////////////////////////////////////////////////////
//Number to Byte[] conversions
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////
//Integer
//////////////////////////////////////////////////
public static byte[] intToBytes(int i){
return intToBytes(i, 4, ByteOrder.LITTLE_ENDIAN);
}
@ -134,16 +138,113 @@ public class ByteConverter {
public static byte[] intToBytes(int i ,int size){
return intToBytes(i, size, ByteOrder.LITTLE_ENDIAN);
}
/**
* Converts an Integer to a Byte Array
* @param i the integer to be converted
* @param size Size that the byte array should be
* @param byteOrder the order that the bytes should be ie Big Endian
* @return
*/
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();
return convertNumtoBytes(copy, size, byteOrder, IntegerSize);
}
//////////////////////////////////////////////////
//Long
//////////////////////////////////////////////////
public static byte[] longToBytes(long i){
return longToBytes(i, LongSize, ByteOrder.LITTLE_ENDIAN);
}
public static byte[] longToBytes(long i ,int size){
return longToBytes(i, size, ByteOrder.LITTLE_ENDIAN);
}
/**
* Converts an Long to a Byte Array
* @param i the Long to be converted
* @param size Size that the byte array should be
* @param byteOrder the order that the bytes should be ie Big Endian
* @return
*/
public static byte[] longToBytes(long i ,int size, ByteOrder byteOrder){
ByteBuffer buffer = ByteBuffer.allocate(LongSize);
buffer.order(byteOrder);
buffer.putLong(i);
byte[] copy = buffer.array();
return convertNumtoBytes(copy, size, byteOrder, LongSize);
}
//////////////////////////////////////////////////
//Short
//////////////////////////////////////////////////
public static byte[] shortToBytes(short i){
return shortToBytes(i, ShortSize, ByteOrder.LITTLE_ENDIAN);
}
public static byte[] shortToBytes(short i ,int size){
return shortToBytes(i, size, ByteOrder.LITTLE_ENDIAN);
}
/**
* Converts an Short to a Byte Array
* @param i the Short to be converted
* @param size Size that the byte array should be
* @param byteOrder the order that the bytes should be ie Big Endian
* @return
*/
public static byte[] shortToBytes(short i ,int size, ByteOrder byteOrder){
ByteBuffer buffer = ByteBuffer.allocate(ShortSize);
buffer.order(byteOrder);
buffer.putShort(i);
byte[] copy = buffer.array();
return convertNumtoBytes(copy, size, byteOrder, ShortSize);
}
//////////////////////////////////////////////////
//Char
//////////////////////////////////////////////////
public static byte[] charToBytes(char i){
return charToBytes(i, CharSize, ByteOrder.LITTLE_ENDIAN);
}
public static byte[] charToBytes(char i ,int size){
return charToBytes(i, size, ByteOrder.LITTLE_ENDIAN);
}
/**
* Converts an Char to a Byte Array
* @param i the Char to be converted
* @param size Size that the byte array should be
* @param byteOrder the order that the bytes should be ie Big Endian
* @return
*/
public static byte[] charToBytes(char i ,int size, ByteOrder byteOrder){
ByteBuffer buffer = ByteBuffer.allocate(CharSize);
buffer.order(byteOrder);
buffer.putChar(i);
byte[] copy = buffer.array();
return convertNumtoBytes(copy, size, byteOrder, CharSize);
}
//////////////////////////////////////////////////
//Conversion Function
//////////////////////////////////////////////////
private static byte[] convertNumtoBytes(byte[] copy ,int size, ByteOrder byteOrder, int fullsize){
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);
bytes = Arrays.copyOfRange(copy, fullsize - size, fullsize);
}
return bytes;
}

@ -15,35 +15,6 @@ import static org.junit.Assert.assertTrue;
*/
public class ByteConverterTest {
@Test
public void testIntToByte(){
int int1 = 100; //100 in bytes
byte[] bytes1 = {100, 0, 0, 0};//this is in little endian
assertTrue(testArrayContents(ByteConverter.intToBytes(int1), bytes1));
byte[] bytes2 = {0, 0, 0, 100};// this is big endian
assertTrue(testArrayContents(ByteConverter.intToBytes(int1, ByteConverter.IntegerSize, ByteOrder.BIG_ENDIAN), bytes2));
//test chopping
byte[] chopped1 = ByteConverter.intToBytes(int1, 3, ByteOrder.LITTLE_ENDIAN);
byte[] bytes3 = {100, 0, 0};
assertTrue(testArrayContents(chopped1, bytes3));
byte[] chopped2 = ByteConverter.intToBytes(int1, 2, ByteOrder.LITTLE_ENDIAN);
byte[] bytes4 = {100, 0};
assertTrue(testArrayContents(chopped2, bytes4));
byte[] chopped3 = ByteConverter.intToBytes(int1, 1, ByteOrder.LITTLE_ENDIAN);
byte[] bytes5 = {100};
assertTrue(testArrayContents(chopped3, bytes5));
byte[] chopped4 = ByteConverter.intToBytes(int1, 3, ByteOrder.BIG_ENDIAN);
byte[] bytes6 = {0, 0, 100};
assertTrue(testArrayContents(chopped4, bytes6));
byte[] chopped5 = ByteConverter.intToBytes(int1, 2, ByteOrder.BIG_ENDIAN);
byte[] bytes7 = {0, 100};
assertTrue(testArrayContents(chopped5, bytes7));
byte[] chopped6 = ByteConverter.intToBytes(int1, 1, ByteOrder.BIG_ENDIAN);
byte[] bytes8 = {100};
assertTrue(testArrayContents(chopped6, bytes8));
}
@Test
public void testLargerIntToByte(){
int int1 = 1532158456; //100 in bytes
@ -98,32 +69,85 @@ public class ByteConverterTest {
}
@Test
public void testIntToLong(){/*
long lng1 = 15; //100 in bytes
byte[] bytes1 = {15, 0, 0, 0, 0, 0, 0, 0};//this is in little endian
assertTrue(testArrayContents(ByteConverter.longToBytes(lng1), bytes1));
byte[] bytes2 = {0, 0, 0, 0, 0, 0, 0, 15};// this is big endian
assertTrue(testArrayContents(ByteConverter.lngToBytes(lng1, ByteConverter.IntegerSize, ByteOrder.BIG_ENDIAN), bytes2));
public void testByteToShort(){
short short1 = 20; //100 in bytes
byte[] bytes1 = {20, 0};//this is in little endian
assertTrue(ByteConverter.bytesToShort(bytes1) == short1);
assertTrue(ByteConverter.bytesToShort(bytes1, ByteOrder.LITTLE_ENDIAN) == short1);
byte[] bytes2 = {0, 20};// this is big endian
assertTrue(ByteConverter.bytesToShort(bytes2, ByteOrder.BIG_ENDIAN) == short1);
//check single bytes to integers
assertTrue(ByteConverter.bytesToShort((byte)20) == short1);
}
@Test
public void testByteToChar(){
char char1 = 20; //100 in bytes
byte[] bytes1 = {20, 0};//this is in little endian
assertTrue(ByteConverter.bytesToChar(bytes1) == char1);
assertTrue(ByteConverter.bytesToChar(bytes1, ByteOrder.LITTLE_ENDIAN) == char1);
byte[] bytes2 = {0, 20};// this is big endian
assertTrue(ByteConverter.bytesToChar(bytes2, ByteOrder.BIG_ENDIAN) == char1);
//check single bytes to integers
assertTrue(ByteConverter.bytesToChar((byte)20) == char1);
}
@Test
public void testIntToByte(){
int int1 = 100; //100 in bytes
byte[] bytes1 = {100, 0, 0, 0};//this is in little endian
assertTrue(testArrayContents(ByteConverter.intToBytes(int1), bytes1));
byte[] bytes2 = {0, 0, 0, 100};// this is big endian
assertTrue(testArrayContents(ByteConverter.intToBytes(int1, ByteConverter.IntegerSize, ByteOrder.BIG_ENDIAN), bytes2));
//test chopping
byte[] chopped1 = ByteConverter.lngToBytes(lng1, 3, ByteOrder.LITTLE_ENDIAN);
byte[] chopped1 = ByteConverter.intToBytes(int1, 3, ByteOrder.LITTLE_ENDIAN);
byte[] bytes3 = {100, 0, 0};
assertTrue(testArrayContents(chopped1, bytes3));
byte[] chopped2 = ByteConverter.lngToBytes(lng1, 2, ByteOrder.LITTLE_ENDIAN);
byte[] chopped2 = ByteConverter.intToBytes(int1, 2, ByteOrder.LITTLE_ENDIAN);
byte[] bytes4 = {100, 0};
assertTrue(testArrayContents(chopped2, bytes4));
byte[] chopped3 = ByteConverter.lngToBytes(lng1, 1, ByteOrder.LITTLE_ENDIAN);
byte[] chopped3 = ByteConverter.intToBytes(int1, 1, ByteOrder.LITTLE_ENDIAN);
byte[] bytes5 = {100};
assertTrue(testArrayContents(chopped3, bytes5));
byte[] chopped4 = ByteConverter.lngToBytes(lng1, 3, ByteOrder.BIG_ENDIAN);
byte[] chopped4 = ByteConverter.intToBytes(int1, 3, ByteOrder.BIG_ENDIAN);
byte[] bytes6 = {0, 0, 100};
assertTrue(testArrayContents(chopped4, bytes6));
byte[] chopped5 = ByteConverter.lngToBytes(lng1, 2, ByteOrder.BIG_ENDIAN);
byte[] chopped5 = ByteConverter.intToBytes(int1, 2, ByteOrder.BIG_ENDIAN);
byte[] bytes7 = {0, 100};
assertTrue(testArrayContents(chopped5, bytes7));
byte[] chopped6 = ByteConverter.lngToBytes(lng1, 1, ByteOrder.BIG_ENDIAN);
byte[] chopped6 = ByteConverter.intToBytes(int1, 1, ByteOrder.BIG_ENDIAN);
byte[] bytes8 = {100};
assertTrue(testArrayContents(chopped6, bytes8));*/
assertTrue(testArrayContents(chopped6, bytes8));
}
@Test
public void testLongToBytes(){
long lng1 = 15; //100 in bytes
byte[] bytes1 = {15, 0, 0, 0, 0, 0, 0, 0};//this is in little endian
assertTrue(testArrayContents(ByteConverter.longToBytes(lng1), bytes1));
byte[] bytes2 = {0, 0, 0, 0, 0, 0, 0, 15};// this is big endian
assertTrue(testArrayContents(ByteConverter.longToBytes(lng1, ByteConverter.LongSize, ByteOrder.BIG_ENDIAN), bytes2));
//test chopping
byte[] chopped1 = ByteConverter.longToBytes(lng1, 3, ByteOrder.LITTLE_ENDIAN);
byte[] bytes3 = {15, 0, 0};
assertTrue(testArrayContents(chopped1, bytes3));
byte[] chopped2 = ByteConverter.longToBytes(lng1, 2, ByteOrder.LITTLE_ENDIAN);
byte[] bytes4 = {15, 0};
assertTrue(testArrayContents(chopped2, bytes4));
byte[] chopped3 = ByteConverter.longToBytes(lng1, 1, ByteOrder.LITTLE_ENDIAN);
byte[] bytes5 = {15};
assertTrue(testArrayContents(chopped3, bytes5));
byte[] chopped4 = ByteConverter.longToBytes(lng1, 3, ByteOrder.BIG_ENDIAN);
byte[] bytes6 = {0, 0, 15};
assertTrue(testArrayContents(chopped4, bytes6));
byte[] chopped5 = ByteConverter.longToBytes(lng1, 2, ByteOrder.BIG_ENDIAN);
byte[] bytes7 = {0, 15};
assertTrue(testArrayContents(chopped5, bytes7));
byte[] chopped6 = ByteConverter.longToBytes(lng1, 1, ByteOrder.BIG_ENDIAN);
byte[] bytes8 = {15};
assertTrue(testArrayContents(chopped6, bytes8));
}
public boolean testArrayContents(byte[] bytes1, byte[] bytes2){

Loading…
Cancel
Save