I have 16 bits. In each bit I can set some property and send to COM port(fiscal printer). For example: if 0 bit checked, then show logo on check.
This 16 bits I need convert to 4 bytes and send to COM port. For example: if 0 bit checked, 4 bytes will be 0x30, 0x31, 0x30, 0x30. Bytes result I get with COM port monitoring API.
What I must to do, to get 4 bytes from 16 bits?
Other examples:
Note that 0x30 = '0' in ASCII. It looks to me like you're transmitting the sixteen bits as two bytes of hex, with bits 0-7 first and the 8-15 second, i.e. you want to transmit
We'd need more data points to be sure, but this fits your examples above:
bit 0 set encodes to string "0100" = 0x30 0x31 0x30 0x30
bit 1 set "0200" = 0x30 0x32 0x30 0x30
bit 2 set "0400" = 0x30 0x34 0x30 0x30
0+2 "0500" = 0x30 0x30 0x30 0x30
0+9 "0102" = 0x30 0x31 0x30 0x32
0,1,2,3,4,5,9 "3F02" = 0x33 0x46 0x30 0x32
i.e. in Java if you have your bits in a single integer n you probably want
String output = Integer.toHexString((n >> 4) & 0xf)
+ Integer.toHexString(n & 0xf)
+ Integer.toHexString((n >> 12) & 0xf)
+ Integer.toHexString((n >> 8) & 0xf);
byte[] data = output.toUpperCase().getBytes("ASCII");
via a string, or
byte[] data = new byte[4];
data[0] = (byte)((n >> 4) & 0xf);
data[1] = (byte)(n & 0xf);
data[2] = (byte)((n >> 12) & 0xf);
data[3] = (byte)((n >> 8) & 0xf);
for(int i = 0; i < 4; ++i) {
data[i] += (data[i] < 10) ? '0' : ('A' - 10);
}
avoiding the string.
To parse the four bytes back into a single int you could use
int bits = (((data[0] & 0xf) + ((data[0] >= 'A') ? 9 : 0)) << 4)
| ((data[1] & 0xf) + ((data[1] >= 'A') ? 9 : 0))
| (((data[2] & 0xf) + ((data[2] >= 'A') ? 9 : 0)) << 12)
| (((data[3] & 0xf) + ((data[3] >= 'A') ? 9 : 0)) << 8);
Obviously there's no input checking here - I'm assuming we get the input in the expected format. The main bit in brackets should just parses a single hex digit out of the string - you could refactor that or implement something more robust if you wanted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With