Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

converting C++ type struct to Java class?

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey there,
can you tell me what this means? and how I can create a Java class to represent this struct?
#pragma pack( push, 1 )
typedef struct _StrmHeader
{
BYTE bIdType : 4;
BYTE bValueLengthSize : 2;
BYTE bIdLengthSize : 2;
BYTE bValueType;
}
StrmHeader;
#pragma pack( pop )
thanks.
sarone
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you have here is a bitfield. bIdType is a four bit number, bValueLengthSize is a two bit number, and bIdLengthSize is a two bit number. I'm a little rusty on bitfields, but I think bValueType is a regular 8 bit byte, assuming that's what BYTE is typedef'd to (it might be typedef'd to int, with the target being a machine on which an int is 8 bits). The pack pragmas are probably to make the struct byte aligned. Note that pragmas and bitfield implementations are very platform dependent, so these are really only educated guesses.
Without knowing the application, it's hard to say exactly what the best Java translation is. Personally, I'd probably implement it as four full byte data members, even though that's less space efficient. The constructor might need to extract these member values from the two byte value that is at the start of the relevant stream (if that's what 'strm' stands for).
 
Sarone Thach
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I have done a little more reserch, I hope this is close to correct.
Here is the equvilent java class:

I'm not entirely sure if the bit shifting is correct and if I'm &ing it with the correct values. If anyone can spot a problem let me know. thanks.
 
Warren Dew
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks pretty good. The "& 0x30" and "& 0xC0" should both instead be "& 0x03", though, since you're shifting the relevant bits to the least significant locations.
Also, which fields need to be shifted are dependent on the particular c implementation you are working with. If the bitfield is filled from the big end instead of from the little end, the shifts will need to be 4, 2, 0 rather than 0, 4, 6. Probably the easiest way to figure out which is correct is to test one way, and if it doesn't work, test the other.
Good luck!
 
Sarone Thach
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Warren, thanks alot for your help, it has given me hints on how to understand this thing. I think I fully understand this now. Here is my final class.
I have assumed that the data I'm getting is big-endian. I just wanted to understand what the struct meant in java. So if little-endian reverse the shifting (<<

I think this is correct. I hope this helps others.
reply
    Bookmark Topic Watch Topic
  • New Topic