Java bit unpacker not matching up with C

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.


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] = ''; 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; }


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] = ''; 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; } } }


You most likely have stumbled upon a very common problem with endianness. C and java might have diffrent ways of representing an int depending on the architecture of the machine running the c-code.

Typically c-programs should convert multi-byte integers into network byte order to simplify portability work like the one your doing right now.

To get the byte-ordering correct you'll have to shift your bits around using the bit-shift and "and" operators.

Also of interest - the nio package has functionality for easily handle LSB byte-order integers typically produced by legacy c-code.

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.

Category:java Time:2011-06-22 Views:2
Tags: java pack bit

Related post

  • Java regular expression to match patterns and extract them 2009-09-09

    I tried writing a program in Java using regex to match a pattern and extract it. Given a string like "This is a link- and this is another" I should be able to get and strings extracted. Here

  • Looking for Java Regular expression to match last 2 digits in the given range 2015-03-10

    I am trying to write a java regular expression to match last 2 digits of a 12 digit number to a range of digits. So for example I want to match the given 12-digit number whose last 2 digits falls in the range of (00-24) or (25-49) or (50-74) or (75-9

  • What are the Java regular expressions for matching IPv4 and IPv6 strings? 2008-09-05

    Looking for a string to pass to String#matches(String) that will match IPv4, and another to match IPv6. --------------Solutions------------- public static final String IPV4_REGEX = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d

  • Java regular expression to match _all_ whitespace characters 2009-11-30

    I'm looking for a regular expression in Java which matches all whitespace characters in a String. "\s" matches only some, it does not match &nbsp; and similar non-ascii whitespaces. I'm looking for a regular expression which matches all (common)

  • Java RegEx Pattern not matching (works in .NET) 2010-04-27

    Below is an example that is producing an exception in Java (and not matching the input). Perhaps I misunderstood the JavaDoc, but it looks like it should work. Using the same pattern and input in C# will produce a match. import java.util.regex.Matche

  • What is the base open source java package to filter/match URLs? 2010-06-10

    I have an high performance application which deals with URLs. For every URL it needs to retrieve the appropriate settings from a predefined pool. Every settings object is associated with a URL pattern which indicates which URLs should use these setti

  • Java regex: Get the matched sequence 2010-06-29

    In Perl/PHP regex is's possible to match a sequence and get an array with matched sequences: preg_match('/20[0-1][0-9]/', $inputstring, $array_return); // PHP I can't figure out how to do this in Java. returns the whole string. Is this

  • Using Java regex want to match only & but not & < etc 2011-02-16

    Using Java regex pattern I want to match the & symbol. But it should not match &amp;, &lt; etc... (HTML encoding). --------------Solutions------------- Try this: &(?![A-Za-z]+;|#[0-9]+;) This matches an & that's not followed by on

  • Java MD5 Hash Not Matching .NET Hash 2011-03-23

    I have a webservice that is written in C# handling some validation of values. In it I need to check a MD5 hash generated in the calling Java client. The Java client generates the hash in this manner Charset utf8Charset = Charset.forName("UTF-8"); byt

  • Java Reflection parameter type matching, how-to handle interfaces instead classes 2011-04-03

    Given a class Something with a constructor public Something(List<String> l), I would like to use klass.getConstructor(parameterTypes) where parameterTypes[0] is the class java.util.ArrayList (because I need to "match" a given specific instance)

  • Java Regex won't match, any explanations? 2011-04-25

    The regex String : "[Ff][uU][Nn][Cc] " Matches input: "fUnC " But not: "func across( a, b )" And I don't understand why... I'm testing my expressions here: I figured out that I (dumbly) needed my regex to

  • using java to unpack binary php packed float 2011-06-21

    I created some binary data using the php function pack. Then I needed to unpack this data from a java application. I've been able of unpacking integers, strings, longs and other data types but I'm in trouble with floats. I create the data from php us

  • Java regular expression longest match 2011-06-24

    I am having a problem with a generic regex that matches (sort of) a typical string of the form ... "field1" "field2" "field3" "field4" ... What I want to do is, of course, get each of these fields separately. Because the fields can contain any charac

  • Java 6 regex multiple matches of one group 2011-08-27

    Here is simple pattern: [key]: [value1] [value2] [value3] [valueN] I want to get: key array of values Here is my regex: ^([^:]+):(:? ([^ ]+))++$ Here is my text: foo: a b c d Matcher gives me 2 groups: foo (as key) and d (as values). If I use +? inst

  • Java, regular expressions, String#matches(String) 2011-09-29

    I'm used to regular expressions from Perl. Does anybody know why this doesn't work (eg. "yea" isn't printed)? if ("zip".matches("ip")) System.out.println("yea"); Thank you. --------------Solutions------------- matches() is a complete match; the strin

  • java regexp for reluctant matching 2012-02-23

    need to find an expression for the following problem: String given = "{ \"questionID\" :\"4\", \"question\":\"What is your favourite hobby?\",\"answer\" :\"answer 4\"},{ \"questionID\" :\"5\", \"question\" :\"What was the name of the first company yo

  • How to make a java regular expression which matches a word in any language 2010-12-15

    To match a word in english I would use pattern [a-zA-Z]+. Is there any way how to write a regular expression which will match a word in any language? That is even if the word contains characters like ščžé.... I have no idea what possible characters e

  • java - How to find matching objects between two lists? 2011-02-15

    Given two Lists, each list holding the same object type, I would like to find objects between the two lists that match, based on some property values. e.g. an object from List1, L1Obj, matches an object from List2, L2Obj, if L1Obj.a == L2Obj.a AND L1

  • Java RegEx: Very Specific Match 2011-03-20

    I'm trying to match a hyphen in all situations except when it is preceded by whitespace and proceeded by non-whitespace. x-y //match x - y //match x- y //match x -y //not match I want the regex to determine a match based on the above requirements but

Copyright (C), All Rights Reserved.

processed in 0.245 (s). 13 q(s)