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 6063133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:07:52+00:00 2026-05-23T09:07:52+00:00

So I have been given a C program, which I am trying to make

  • 0

So I have been given a C program, which I am trying to make more portable by translating to Java, which has been going well. However, I have run into a bit of hangup, which I believe lies in this bit of code. It just a function that unpacks a 30 byte frame. I have a feeling I am running into issues with the variable types, trying to figure out the best way to sub Java types for some that don’t carry over from C. If anyone sees where the issue may be, I would appreciate it.

Java:

public static byte[] unPackBits(byte[] Src, int bitOffset, byte[] Dst, int bitCount){

    int srcByteOffset, srcBit;
    int dstByteOffset, dstBit;
    char dstMask, srcMask;

    srcByteOffset = bitOffset / 8;
    srcBit = bitOffset % 8;
    srcMask = (char)(0x01<<srcBit);

    dstByteOffset = 0;
    dstBit = 0;
    dstMask = 0x01;

    Dst[dstByteOffset] = '\0';

    for(int b = 0; b < bitCount; b++){
        if((Src[srcByteOffset] & srcMask) != (char)0x00){
            Dst[dstByteOffset] = (byte)(Dst[dstByteOffset]|dstMask);
        }
        else {
            Dst[dstByteOffset] = (byte)(Dst[dstByteOffset] & (~dstMask));
        }

        srcBit++;
        if(srcBit < 8) {
            srcMask = (char)(srcMask<<1);
        }
        else {
            srcByteOffset++;
            srcBit = 0;
            srcMask = 0x01;
        }

        dstBit++;
        if(dstBit < 8) {
            dstMask = (char)(dstMask<<1);
        }
        else {
            dstByteOffset++;
            dstMask = 0x01;
            dstBit = 0;
        }
    }
    return Dst;
}

C:

void TRB::unPackBits(char *Src, int BitOffset, char *Dst, int BitCount) {
   int srcByteOffset, srcBit;
   int dstByteOffset, dstBit;
   char dstMask, srcMask;

   srcByteOffset   = BitOffset / 8;
   srcBit          = BitOffset % 8;
   srcMask = 0x01<<srcBit;

   dstByteOffset = 0;
   dstBit  = 0;
   dstMask = 0x01;
   Dst[dstByteOffset] = '\0';


   for(int b = 0; b < BitCount; b++) {
      if((Src[srcByteOffset] & srcMask) != (char)0x00) {
         Dst[dstByteOffset] = Dst[dstByteOffset] | dstMask;
      }
      else {
         Dst[dstByteOffset] = Dst[dstByteOffset] & (~dstMask);
      }

      srcBit++;
      if(srcBit < 8) {
         srcMask = srcMask<<1;
      }
      else {
         srcByteOffset++;
         srcBit = 0;
         srcMask = 0x01;
      }

      dstBit++;
      if(dstBit < 8) {
         dstMask = dstMask<<1;
      }
      else {
         dstByteOffset++;
         dstMask = 0x01;
         dstBit = 0;
      }
   }

}
  • 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-05-23T09:07:53+00:00Added an answer on May 23, 2026 at 9:07 am

    The problem is that C doesn’t have a built in way to easily access data inside a char *. The unPackBits function is just extracting data from a certain location (BitOffset) with a certain length (BitCount) and returning it in the same form. Then that data has to be cast to the appropriate types, be it bool, int, double, etc.

    Java, on the other hand has a built in way to handle the same issue. Data that is stored in a byte array can be accessed by using ByteBuffer.wrap(byte[]). ByteBuffer has functions that return int, double, long, short, float, and char, as well as ones to put all of those types and a few other useful functions, like translating the entire structure into an int array. Each of these only need to be told the offset, they hold in memory the BitCount. The only primitive data type it doesn’t handle is boolean, so I will still be unpacking with the byte array, not a problem since I know exactly where in the array those values are held.

    As for the bit shifting, shift the entire array before wrapping it as a ByteBuffer, as suggested by @vidstige, in the same way the first if else in the for loop does, and everything will be in order.

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

Sidebar

Related Questions

I have been given a project where i need to write a program which
I have been given two different Microsoft Word document that my virus scanner has
Have recently been given a project to complete which uses XML quite extensively.Am looking
I have been tasked to do a scheduling program in my company. Basically given
I am currently learning C++ and I have been asked to make a program
I have a small program that which is confusing me. I am trying using
This is my first attempt at java problem I have been given as part
I have been given a requirement where I need to support multiple databases in
I have been given a DLL (InfoLookup.dll) that internally allocates structures and returns pointers
I am given a problem where I have been given N nodes in a

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.