Skip to content

Commit

Permalink
add method and tests
Browse files Browse the repository at this point in the history
Add method bytesToStringNoSpace for byte and  setBit
  • Loading branch information
devnied committed Jul 5, 2014
1 parent 36faf29 commit fa586da
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 3 deletions.
44 changes: 41 additions & 3 deletions src/main/java/fr/devnied/bitlib/BytesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static int byteArrayToInt(final byte[] byteArray) {
* start position in array in the
* @param length
* length of data
* @return
* @return int value of byte array
*/
public static int byteArrayToInt(final byte[] byteArray, final int startPos, final int length) {
if (byteArray == null) {
Expand Down Expand Up @@ -101,6 +101,17 @@ public static String bytesToString(final byte[] pBytes, final boolean pTruncate)
return formatByte(pBytes, FORMAT_SPACE, pTruncate);
}

/**
* Method to convert byte to string without space between byte
*
* @param pByte
* byte to convert
* @return a string
*/
public static String bytesToStringNoSpace(final byte pByte) {
return formatByte(new byte[] { pByte }, FORMAT_NOSPACE, false);
}

/**
* Method to convert bytes to string without space between bytes
*
Expand Down Expand Up @@ -177,15 +188,18 @@ public static byte[] fromString(final String pData) {
}

/**
* Test if bit at given index of given value is = 1
* Test if bit at given index of given value is = 1.
*
* @param pVal
* value to test
* @param pBitIndex
* bit index
* bit index between 0 and 7
* @return true bit at given index of give value is = 1
*/
public static boolean matchBitByBitIndex(final int pVal, final int pBitIndex) {
if (pBitIndex < 0 || pBitIndex > 7) {
throw new IllegalArgumentException("parameter 'pBitIndex' must be between 0 and 7. pBitIndex=" + pBitIndex);
}
return (pVal & 1 << pBitIndex) != 0;
}

Expand All @@ -202,6 +216,30 @@ public static boolean matchBitByValue(final int pInitialValue, final int pValueT
return matchBitByBitIndex(pInitialValue, MAX_BIT_INTEGER - Integer.numberOfLeadingZeros(pValueToCompare));
}

/**
* Method used to set a bit index to 1 or 0.
*
* @param data
* data to modify
* @param pBitIndex
* index to set
* @param pOn
* set bit at specified index to 1 or 0
* @return the modified byte
*/
public static byte setBit(final byte pData, final int pBitIndex, final boolean pOn) {
if (pBitIndex < 0 || pBitIndex > 7) {
throw new IllegalArgumentException("parameter 'pBitIndex' must be between 0 and 7. pBitIndex=" + pBitIndex);
}
byte ret = pData;
if (pOn) { // Set bit
ret |= 1 << pBitIndex;
} else { // Unset bit
ret &= ~(1 << pBitIndex);
}
return ret;
}

/**
* Convert byte array to binary String
*
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/fr/devnied/bitlib/BytesUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ public void testBytesToStringNoSpace() {
Assertions.assertThat(BytesUtils.bytesToStringNoSpace(tab2)).isEqualTo("00010245");
}

/**
*
*/
@Test
public void testBytesToStringNoSpacebyte() {
Assertions.assertThat(BytesUtils.bytesToStringNoSpace((byte) 0)).isEqualTo("00");
Assertions.assertThat(BytesUtils.bytesToStringNoSpace((byte) 255)).isEqualTo("FF");
Assertions.assertThat(BytesUtils.bytesToStringNoSpace(null)).isEqualTo("");
}

/**
*
*/
Expand Down Expand Up @@ -157,6 +167,53 @@ public void testMatchBit() {
Assertions.assertThat(BytesUtils.matchBitByValue(10, 1)).isEqualTo(false);
}

/**
*
*/
@Test
public void testMatchBitByBitIndex() {
Assertions.assertThat(BytesUtils.matchBitByBitIndex(1, 0)).isEqualTo(true);
Assertions.assertThat(BytesUtils.matchBitByBitIndex(128, 7)).isEqualTo(true);
Assertions.assertThat(BytesUtils.matchBitByBitIndex(127, 7)).isEqualTo(false);
Assertions.assertThat(BytesUtils.matchBitByBitIndex(255, 7)).isEqualTo(true);
Assertions.assertThat(BytesUtils.matchBitByBitIndex(0, 0)).isEqualTo(false);

try {
BytesUtils.matchBitByBitIndex(0, -1);
Assert.fail();
} catch (Exception e) {
}
try {
BytesUtils.matchBitByBitIndex(0, 8);
Assert.fail();
} catch (Exception e) {
}
}

/**
*
*/
@Test
public void testSetBytes() {
byte max = (byte) 0xFF;
byte min = 0;
Assertions.assertThat(BytesUtils.bytesToStringNoSpace(BytesUtils.setBit(min, 7, true))).isEqualTo("80");
Assertions.assertThat(BytesUtils.bytesToStringNoSpace(BytesUtils.setBit(min, 0, true))).isEqualTo("01");
Assertions.assertThat(BytesUtils.bytesToStringNoSpace(BytesUtils.setBit(max, 4, false))).isEqualTo("EF");
Assertions.assertThat(BytesUtils.bytesToStringNoSpace(BytesUtils.setBit(max, 0, false))).isEqualTo("FE");
Assertions.assertThat(BytesUtils.bytesToStringNoSpace(BytesUtils.setBit(min, 0, false))).isEqualTo("00");
try {
BytesUtils.setBit(max, -1, false);
Assert.fail();
} catch (Exception e) {
}
try {
BytesUtils.setBit(max, 8, false);
Assert.fail();
} catch (Exception e) {
}
}

/**
*
*/
Expand Down

0 comments on commit fa586da

Please sign in to comment.