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:
- 1 bit checked – 0x30, 0x32, 0x30, 0x30
- 2 bit checked – 0x30, 0x34, 0x30, 0x30
- 0 and 2 bits checked – 0x30, 0x35, 0x30, 0x30
- 0 and 9 bits checked – 0x30, 0x31, 0x30, 0x32
- 0,1,2,3,4,5,9 bits checked – 0x33, 0x46, 0x30, 0x32
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:
i.e. in Java if you have your bits in a single integer
nyou probably wantvia a string, or
avoiding the string.
To parse the four bytes back into a single int you could use
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.