Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
BitDemoData.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 * Copyright (c) 2014 Gothel Software e.K.
5 * Copyright (c) 2014 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27package jau.test.util;
28
29import java.nio.ByteBuffer;
30import java.util.Locale;
31
32public class BitDemoData {
33 public static final long UNSIGNED_INT_MAX_VALUE = 0xffffffffL;
34
35 public static final String[] pyramid32bit_one = {
36 "00000000000000000000000000000001",
37 "00000000000000000000000000000010",
38 "00000000000000000000000000000100",
39 "00000000000000000000000000001000",
40 "00000000000000000000000000010000",
41 "00000000000000000000000000100000",
42 "00000000000000000000000001000000",
43 "00000000000000000000000010000000",
44 "00000000000000000000000100000000",
45 "00000000000000000000001000000000",
46 "00000000000000000000010000000000",
47 "00000000000000000000100000000000",
48 "00000000000000000001000000000000",
49 "00000000000000000010000000000000",
50 "00000000000000000100000000000000",
51 "00000000000000001000000000000000",
52 "00000000000000010000000000000000",
53 "00000000000000100000000000000000",
54 "00000000000001000000000000000000",
55 "00000000000010000000000000000000",
56 "00000000000100000000000000000000",
57 "00000000001000000000000000000000",
58 "00000000010000000000000000000000",
59 "00000000100000000000000000000000",
60 "00000001000000000000000000000000",
61 "00000010000000000000000000000000",
62 "00000100000000000000000000000000",
63 "00001000000000000000000000000000",
64 "00010000000000000000000000000000",
65 "00100000000000000000000000000000",
66 "01000000000000000000000000000000",
67 "10000000000000000000000000000000"
68 };
69
70 //
71 // MSB -> LSB over whole data
72 //
73 public static final byte[] testBytesMSB = new byte[] { (byte)0xde, (byte)0xaf, (byte)0xca, (byte)0xfe };
74 public static final int testIntMSB = 0xdeafcafe; // 11011110 10101111 11001010 11111110
75 public static final String[] testStringsMSB = new String[] { "11011110", "10101111", "11001010", "11111110" };
77
78 //
79 // MSB -> LSB, reverse bit-order over each byte of testBytesLSB
80 //
81 public static final byte[] testBytesMSB_rev = new byte[] { (byte)0xfe, (byte)0xca, (byte)0xaf, (byte)0xde };
82 public static final int testIntMSB_rev = 0xfecaafde;
83 public static final String[] testStringsMSB_rev = new String[] { "11111110", "11001010", "10101111", "11011110" };
85
86 //
87 // LSB -> MSB over whole data
88 //
89 public static final byte[] testBytesLSB = new byte[] { (byte)0x7f, (byte)0x53, (byte)0xf5, (byte)0x7b };
90 public static final int testIntLSB = 0x7f53f57b;
91 public static final String[] testStringsLSB = new String[] { "01111111", "01010011", "11110101", "01111011" };
93
94 //
95 // LSB -> MSB, reverse bit-order over each byte of testBytesMSB
96 //
97 public static final byte[] testBytesLSB_revByte = new byte[] { (byte)0x7b, (byte)0xf5, (byte)0x53, (byte)0x7f };
98 public static final int testIntLSB_revByte = 0x7bf5537f;
99 public static final String[] testStringsLSB_revByte = new String[] { "01111011", "11110101", "01010011", "01111111" };
101
102 public static final void dumpData(final String prefix, final byte[] data, final int offset, final int len) {
103 for(int i=0; i<len; ) {
104 System.err.printf("%s: %03d: ", prefix, i);
105 for(int j=0; j<8 && i<len; j++, i++) {
106 final int v = 0xFF & data[offset+i];
107 System.err.printf(toHexBinaryString(v, 8)+", ");
108 }
109 System.err.println("");
110 }
111 }
112 public static final void dumpData(final String prefix, final ByteBuffer data, final int offset, final int len) {
113 for(int i=0; i<len; ) {
114 System.err.printf("%s: %03d: ", prefix, i);
115 for(int j=0; j<8 && i<len; j++, i++) {
116 final int v = 0xFF & data.get(offset+i);
117 System.err.printf(toHexBinaryString(v, 8)+", ");
118 }
119 System.err.println("");
120 }
121 }
122
123 public static int getOneBitCount(final String pattern) {
124 int c=0;
125 for(int i=0; i<pattern.length(); i++) {
126 if( '1' == pattern.charAt(i) ) {
127 c++;
128 }
129 }
130 return c;
131 }
132 public static long toLong(final String bitPattern) {
133 return Long.valueOf(bitPattern, 2).longValue();
134 }
135 public static int toInteger(final String bitPattern) {
136 final long res = Long.valueOf(bitPattern, 2).longValue();
137 if( res > UNSIGNED_INT_MAX_VALUE ) {
138 throw new NumberFormatException("Exceeds "+toHexString(UNSIGNED_INT_MAX_VALUE)+": "+toHexString(res)+" - source "+bitPattern);
139 }
140 return (int)res;
141 }
142
143 public static String toHexString(final int v) {
144 return "0x"+Integer.toHexString(v);
145 }
146 public static String toHexString(final long v) {
147 return "0x"+Long.toHexString(v);
148 }
149 public static final String strZeroPadding= "0000000000000000000000000000000000000000000000000000000000000000"; // 64
150 public static String toBinaryString(final int v, final int bitCount) {
151 if( 0 == bitCount ) {
152 return "";
153 }
154 final int mask = (int) ( ( 1L << bitCount ) - 1L );
155 final String s0 = Integer.toBinaryString( mask & v );
156 return strZeroPadding.substring(0, bitCount-s0.length())+s0;
157 }
158 public static String toBinaryString(final long v, final int bitCount) {
159 if( 0 == bitCount ) {
160 return "";
161 }
162 final long mask = ( 1L << bitCount ) - 1L;
163 final String s0 = Long.toBinaryString( mask & v );
164 return strZeroPadding.substring(0, bitCount-s0.length())+s0;
165 }
166 public static String toHexBinaryString(final long v, final int bitCount) {
167 final int nibbles = 0 == bitCount ? 2 : ( bitCount + 3 ) / 4;
168 return String.format((Locale)null, "[%0"+nibbles+"X, %s]", v, toBinaryString(v, bitCount));
169 }
170 public static String toHexBinaryString(final int v, final int bitCount) {
171 final int nibbles = 0 == bitCount ? 2 : ( bitCount + 3 ) / 4;
172 return String.format((Locale)null, "[%0"+nibbles+"X, %s]", v, toBinaryString(v, bitCount));
173 }
174 public static String toHexBinaryString(final short v, final int bitCount) {
175 final int nibbles = 0 == bitCount ? 2 : ( bitCount + 3 ) / 4;
176 return String.format((Locale)null, "[%0"+nibbles+"X, %s]", v, toBinaryString(v, bitCount));
177 }
178}
static final String strZeroPadding
static int getOneBitCount(final String pattern)
static String toHexBinaryString(final short v, final int bitCount)
static final String testStringMSB_rev
static String toHexString(final long v)
static final void dumpData(final String prefix, final byte[] data, final int offset, final int len)
static final int testIntMSB_rev
static final long UNSIGNED_INT_MAX_VALUE
static final String[] testStringsLSB_revByte
static String toBinaryString(final long v, final int bitCount)
static final String[] pyramid32bit_one
static final String[] testStringsMSB
static String toBinaryString(final int v, final int bitCount)
static String toHexString(final int v)
static String toHexBinaryString(final int v, final int bitCount)
static long toLong(final String bitPattern)
static final String[] testStringsMSB_rev
static final int testIntMSB
static final byte[] testBytesLSB
static final byte[] testBytesLSB_revByte
static final String testStringLSB
static final byte[] testBytesMSB_rev
static String toHexBinaryString(final long v, final int bitCount)
static final int testIntLSB_revByte
static final void dumpData(final String prefix, final ByteBuffer data, final int offset, final int len)
static final String[] testStringsLSB
static final byte[] testBytesMSB
static final String testStringLSB_revByte
static int toInteger(final String bitPattern)
static final String testStringMSB
static final int testIntLSB