Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7740179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:42:39+00:00 2026-06-01T08:42:39+00:00

I have 16 bits. In each bit I can set some property and send

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:42:41+00:00Added an answer on June 1, 2026 at 8:42 am

    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

    • hex digit for bits 4-7
    • hex digit for bits 0-3
    • hex digit for bits 12-15
    • hex digit for bits 8-11

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 4 binary bits Bit 3 Bit 2 Bit 1 Bit 0 Normally
I have a set of 6 bits that represent a 7bit ASCII character. How
I have a bit field consisting of 64 bits: long bitfield = 0; I
I have a command line executable that alters some bits in a file that
I want to count the bits that are set in an extremely large bit-vector
I have a set of points and each one has an area of influence
I have 13 numbers drawing from a set with 13 types of data, each
I have bits of horrible code I have to deal with ... <div class=container>
I would like to sign a device, and I have 64 bits to store
I am using Codeigniter to develop an application, and have been reading bits about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.