diff options
author | CGantert345 <57003061+CGantert345@users.noreply.github.com> | 2022-02-10 14:00:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-10 14:00:06 +0100 |
commit | 7fe844da786691bf839f169bcab8e71b5021329e (patch) | |
tree | dad727a39dc76d330fe28480e24e8a846c36b760 /src/main/java/org/uic/barcode/asn1/uper/AsnUtils.java | |
parent | Merge pull request #41 from UnionInternationalCheminsdeFer/1.2.13 (diff) | |
parent | version number update (diff) | |
download | UIC-barcode-7fe844da786691bf839f169bcab8e71b5021329e.tar UIC-barcode-7fe844da786691bf839f169bcab8e71b5021329e.tar.gz UIC-barcode-7fe844da786691bf839f169bcab8e71b5021329e.tar.bz2 UIC-barcode-7fe844da786691bf839f169bcab8e71b5021329e.tar.lz UIC-barcode-7fe844da786691bf839f169bcab8e71b5021329e.tar.xz UIC-barcode-7fe844da786691bf839f169bcab8e71b5021329e.tar.zst UIC-barcode-7fe844da786691bf839f169bcab8e71b5021329e.zip |
Diffstat (limited to '')
-rw-r--r-- | src/main/java/org/uic/barcode/asn1/uper/AsnUtils.java | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/main/java/org/uic/barcode/asn1/uper/AsnUtils.java b/src/main/java/org/uic/barcode/asn1/uper/AsnUtils.java new file mode 100644 index 0000000..414f181 --- /dev/null +++ b/src/main/java/org/uic/barcode/asn1/uper/AsnUtils.java @@ -0,0 +1,75 @@ +package org.uic.barcode.asn1.uper;
+
+import java.math.BigInteger;
+
+public class AsnUtils {
+
+
+ private static byte[] mask = new byte[] {
+ (byte) 0b1000_0000,
+ 0b0100_0000,
+ 0b0010_0000,
+ 0b0001_0000,
+ 0b0000_1000,
+ 0b0000_0100,
+ 0b0000_0010,
+ 0b0000_0001,
+ };
+
+
+ public static byte[] fromBooleanString(final String s) {
+
+ char[] ascii = s.toCharArray();
+ if (ascii == null || ascii.length == 0) {
+ return null;
+ }
+ // get length/8 times bytes with 3 bit shifts to the right of the length
+ final byte[] l_raw = new byte[ascii.length >> 3];
+ /*
+ * We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the
+ * loop.
+ */
+ for (int ii = 0, jj = 0; ii < l_raw.length; ii++, jj += 8) {
+ for (int bits = 0; bits < mask.length; ++bits) {
+ if (ascii[jj + bits] == '1') {
+ l_raw[ii] |= mask[bits];
+ }
+ }
+ }
+ return l_raw;
+ }
+
+ public static String toBooleanString(byte[] bytes) {
+ StringBuilder sb = new StringBuilder(bytes.length);
+ for (int i = 0; i < bytes.length*8;i++) {
+ sb.append(AsnUtils.get(bytes,i) ? "1" : "0");
+ }
+ return sb.toString();
+ }
+
+ public static boolean get(byte[] bytes, int index) {
+
+ if (index < 0) {
+ throw new IndexOutOfBoundsException("Index " + index + " is less than 0");
+ } else if (index >= bytes.length * 8) {
+ throw new IndexOutOfBoundsException("Index " + index + " violates the limit " + bytes.length*8);
+ }
+ boolean result = (bytes[index / 8] & mask[index % 8]) != 0;
+ return result;
+ }
+
+ public static byte[] shiftBytesToLeft(byte[] bytes, int shift) {
+
+ // create from array
+ BigInteger bigInt = new BigInteger(bytes);
+
+ // shift
+ BigInteger shiftInt = bigInt.shiftLeft(shift);
+
+ // back to array
+ return shiftInt.toByteArray();
+
+ }
+
+
+}
|