Skip to content

Commit fa586da

Browse files
committed
add method and tests
Add method bytesToStringNoSpace for byte and setBit
1 parent 36faf29 commit fa586da

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

src/main/java/fr/devnied/bitlib/BytesUtils.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static int byteArrayToInt(final byte[] byteArray) {
5858
* start position in array in the
5959
* @param length
6060
* length of data
61-
* @return
61+
* @return int value of byte array
6262
*/
6363
public static int byteArrayToInt(final byte[] byteArray, final int startPos, final int length) {
6464
if (byteArray == null) {
@@ -101,6 +101,17 @@ public static String bytesToString(final byte[] pBytes, final boolean pTruncate)
101101
return formatByte(pBytes, FORMAT_SPACE, pTruncate);
102102
}
103103

104+
/**
105+
* Method to convert byte to string without space between byte
106+
*
107+
* @param pByte
108+
* byte to convert
109+
* @return a string
110+
*/
111+
public static String bytesToStringNoSpace(final byte pByte) {
112+
return formatByte(new byte[] { pByte }, FORMAT_NOSPACE, false);
113+
}
114+
104115
/**
105116
* Method to convert bytes to string without space between bytes
106117
*
@@ -177,15 +188,18 @@ public static byte[] fromString(final String pData) {
177188
}
178189

179190
/**
180-
* Test if bit at given index of given value is = 1
191+
* Test if bit at given index of given value is = 1.
181192
*
182193
* @param pVal
183194
* value to test
184195
* @param pBitIndex
185-
* bit index
196+
* bit index between 0 and 7
186197
* @return true bit at given index of give value is = 1
187198
*/
188199
public static boolean matchBitByBitIndex(final int pVal, final int pBitIndex) {
200+
if (pBitIndex < 0 || pBitIndex > 7) {
201+
throw new IllegalArgumentException("parameter 'pBitIndex' must be between 0 and 7. pBitIndex=" + pBitIndex);
202+
}
189203
return (pVal & 1 << pBitIndex) != 0;
190204
}
191205

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

219+
/**
220+
* Method used to set a bit index to 1 or 0.
221+
*
222+
* @param data
223+
* data to modify
224+
* @param pBitIndex
225+
* index to set
226+
* @param pOn
227+
* set bit at specified index to 1 or 0
228+
* @return the modified byte
229+
*/
230+
public static byte setBit(final byte pData, final int pBitIndex, final boolean pOn) {
231+
if (pBitIndex < 0 || pBitIndex > 7) {
232+
throw new IllegalArgumentException("parameter 'pBitIndex' must be between 0 and 7. pBitIndex=" + pBitIndex);
233+
}
234+
byte ret = pData;
235+
if (pOn) { // Set bit
236+
ret |= 1 << pBitIndex;
237+
} else { // Unset bit
238+
ret &= ~(1 << pBitIndex);
239+
}
240+
return ret;
241+
}
242+
205243
/**
206244
* Convert byte array to binary String
207245
*

src/test/java/fr/devnied/bitlib/BytesUtilsTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ public void testBytesToStringNoSpace() {
111111
Assertions.assertThat(BytesUtils.bytesToStringNoSpace(tab2)).isEqualTo("00010245");
112112
}
113113

114+
/**
115+
*
116+
*/
117+
@Test
118+
public void testBytesToStringNoSpacebyte() {
119+
Assertions.assertThat(BytesUtils.bytesToStringNoSpace((byte) 0)).isEqualTo("00");
120+
Assertions.assertThat(BytesUtils.bytesToStringNoSpace((byte) 255)).isEqualTo("FF");
121+
Assertions.assertThat(BytesUtils.bytesToStringNoSpace(null)).isEqualTo("");
122+
}
123+
114124
/**
115125
*
116126
*/
@@ -157,6 +167,53 @@ public void testMatchBit() {
157167
Assertions.assertThat(BytesUtils.matchBitByValue(10, 1)).isEqualTo(false);
158168
}
159169

170+
/**
171+
*
172+
*/
173+
@Test
174+
public void testMatchBitByBitIndex() {
175+
Assertions.assertThat(BytesUtils.matchBitByBitIndex(1, 0)).isEqualTo(true);
176+
Assertions.assertThat(BytesUtils.matchBitByBitIndex(128, 7)).isEqualTo(true);
177+
Assertions.assertThat(BytesUtils.matchBitByBitIndex(127, 7)).isEqualTo(false);
178+
Assertions.assertThat(BytesUtils.matchBitByBitIndex(255, 7)).isEqualTo(true);
179+
Assertions.assertThat(BytesUtils.matchBitByBitIndex(0, 0)).isEqualTo(false);
180+
181+
try {
182+
BytesUtils.matchBitByBitIndex(0, -1);
183+
Assert.fail();
184+
} catch (Exception e) {
185+
}
186+
try {
187+
BytesUtils.matchBitByBitIndex(0, 8);
188+
Assert.fail();
189+
} catch (Exception e) {
190+
}
191+
}
192+
193+
/**
194+
*
195+
*/
196+
@Test
197+
public void testSetBytes() {
198+
byte max = (byte) 0xFF;
199+
byte min = 0;
200+
Assertions.assertThat(BytesUtils.bytesToStringNoSpace(BytesUtils.setBit(min, 7, true))).isEqualTo("80");
201+
Assertions.assertThat(BytesUtils.bytesToStringNoSpace(BytesUtils.setBit(min, 0, true))).isEqualTo("01");
202+
Assertions.assertThat(BytesUtils.bytesToStringNoSpace(BytesUtils.setBit(max, 4, false))).isEqualTo("EF");
203+
Assertions.assertThat(BytesUtils.bytesToStringNoSpace(BytesUtils.setBit(max, 0, false))).isEqualTo("FE");
204+
Assertions.assertThat(BytesUtils.bytesToStringNoSpace(BytesUtils.setBit(min, 0, false))).isEqualTo("00");
205+
try {
206+
BytesUtils.setBit(max, -1, false);
207+
Assert.fail();
208+
} catch (Exception e) {
209+
}
210+
try {
211+
BytesUtils.setBit(max, 8, false);
212+
Assert.fail();
213+
} catch (Exception e) {
214+
}
215+
}
216+
160217
/**
161218
*
162219
*/

0 commit comments

Comments
 (0)