@Test void testEncodeExampleD_1_2() { ByteBuffer buf = ByteBuffer.allocate(16); buf.put((byte) 0x88); buf.put((byte) 0x00); NBitInteger.encode(buf, 5, 1337); buf.flip(); String r = TypeUtils.toHexString(BufferUtils.toArray(buf)); assertEquals("881f9a0a", r); }
public static void encode(ByteBuffer buf, int n, int i) { if (n == 8) { if (i < 0xFF) { buf.put((byte) i); } else { buf.put((byte) 0xFF); int length = i - 0xFF; while (true) { if ((length & ~0x7F) == 0) { buf.put((byte) length); return; } else { buf.put((byte) ((length & 0x7F) | 0x80)); length >>>= 7; } } } } else { int p = buf.position() - 1; int bits = 0xFF >>> (8 - n); if (i < bits) { buf.put(p, (byte) ((buf.get(p) & ~bits) | i)); } else { buf.put(p, (byte) (buf.get(p) | bits)); int length = i - bits; while (true) { if ((length & ~0x7F) == 0) { buf.put((byte) length); return; } else { buf.put((byte) ((length & 0x7F) | 0x80)); length >>>= 7; } } } } }
NBitInteger { public static void encode(ByteBuffer buf, int n, int i) { if (n == 8) { if (i < 0xFF) { buf.put((byte) i); } else { buf.put((byte) 0xFF); int length = i - 0xFF; while (true) { if ((length & ~0x7F) == 0) { buf.put((byte) length); return; } else { buf.put((byte) ((length & 0x7F) | 0x80)); length >>>= 7; } } } } else { int p = buf.position() - 1; int bits = 0xFF >>> (8 - n); if (i < bits) { buf.put(p, (byte) ((buf.get(p) & ~bits) | i)); } else { buf.put(p, (byte) (buf.get(p) | bits)); int length = i - bits; while (true) { if ((length & ~0x7F) == 0) { buf.put((byte) length); return; } else { buf.put((byte) ((length & 0x7F) | 0x80)); length >>>= 7; } } } } } }
NBitInteger { public static void encode(ByteBuffer buf, int n, int i) { if (n == 8) { if (i < 0xFF) { buf.put((byte) i); } else { buf.put((byte) 0xFF); int length = i - 0xFF; while (true) { if ((length & ~0x7F) == 0) { buf.put((byte) length); return; } else { buf.put((byte) ((length & 0x7F) | 0x80)); length >>>= 7; } } } } else { int p = buf.position() - 1; int bits = 0xFF >>> (8 - n); if (i < bits) { buf.put(p, (byte) ((buf.get(p) & ~bits) | i)); } else { buf.put(p, (byte) (buf.get(p) | bits)); int length = i - bits; while (true) { if ((length & ~0x7F) == 0) { buf.put((byte) length); return; } else { buf.put((byte) ((length & 0x7F) | 0x80)); length >>>= 7; } } } } }  }
NBitInteger { public static void encode(ByteBuffer buf, int n, int i) { if (n == 8) { if (i < 0xFF) { buf.put((byte) i); } else { buf.put((byte) 0xFF); int length = i - 0xFF; while (true) { if ((length & ~0x7F) == 0) { buf.put((byte) length); return; } else { buf.put((byte) ((length & 0x7F) | 0x80)); length >>>= 7; } } } } else { int p = buf.position() - 1; int bits = 0xFF >>> (8 - n); if (i < bits) { buf.put(p, (byte) ((buf.get(p) & ~bits) | i)); } else { buf.put(p, (byte) (buf.get(p) | bits)); int length = i - bits; while (true) { if ((length & ~0x7F) == 0) { buf.put((byte) length); return; } else { buf.put((byte) ((length & 0x7F) | 0x80)); length >>>= 7; } } } } }  static int octectsNeeded(int n, int i); static void encode(ByteBuffer buf, int n, int i); static int decode(ByteBuffer buffer, int n); }
NBitInteger { public static void encode(ByteBuffer buf, int n, int i) { if (n == 8) { if (i < 0xFF) { buf.put((byte) i); } else { buf.put((byte) 0xFF); int length = i - 0xFF; while (true) { if ((length & ~0x7F) == 0) { buf.put((byte) length); return; } else { buf.put((byte) ((length & 0x7F) | 0x80)); length >>>= 7; } } } } else { int p = buf.position() - 1; int bits = 0xFF >>> (8 - n); if (i < bits) { buf.put(p, (byte) ((buf.get(p) & ~bits) | i)); } else { buf.put(p, (byte) (buf.get(p) | bits)); int length = i - bits; while (true) { if ((length & ~0x7F) == 0) { buf.put((byte) length); return; } else { buf.put((byte) ((length & 0x7F) | 0x80)); length >>>= 7; } } } } }  static int octectsNeeded(int n, int i); static void encode(ByteBuffer buf, int n, int i); static int decode(ByteBuffer buffer, int n);  }