Abhisek
Abhisek

Reputation: 715

Need to create a class in Java like a structure in C

I have a structure in "C" like this

typedef struct SKT_SubHeader_s
{
        U32 subLength;
        U8 subCode;
        U8 subType;
        U8 subResult;
        U8 subCause;
        U8 subSrcMSISDN[RBT_SUBSCRIBER_NO_LEN];
        U8 subDstMSISDN[RBT_SUBSCRIBER_NO_LEN];
        S8 subRsv[20];
        S16 subSrvType;
        S16 subSrvId;
        S16 subSndType;
        S16 subSndIdx;
        S8 subSndCode[RBT_TONE_ID_LEN];
        S8 subSndValue[RBT_TONE_ID_LEN];
        S32 subMsgId;
        U8 subBillFlag;
        S16 subGroupOrder;
        S8 subDate[9];
        S8 subGroupName[31];
        U8 subSpFlag;
        S8 subBillCode[21];
        S8 subReserve[30];
        U32 bodyLength;
        SKT_body_t bodyData[1];
}SKT_SubHeader_t;

I need to create a same type of a java class whose value i can access from outside and able to write in a file.

Upvotes: 1

Views: 12997

Answers (5)

Eamonn McEvoy
Eamonn McEvoy

Reputation: 8986

Create an object of this class and you will be able to access each member.

Main.java

Skt_Subheader_s SKT_SubHeader_t = new SKT_SubHeader_s;
SKT_SubHeader_t.subLength = 10;

SKT_SubHeader_s.java

public class SKT_SubHeader_s
{
   public int subLength;
   public byte subCode;
   public byte subType;
   public byte subResult;
   public byte subCause;
   public byte subSrcMSISDN[RBT_SUBSCRIBER_NO_LEN];
   public byte subDstMSISDN[RBT_SUBSCRIBER_NO_LEN];
   public byte subRsv[20];
   public string subSrvType;
   public string subSrvId;
   public string subSndType;
   public string subSndIdx;
   public string subSndCode[RBT_TONE_ID_LEN];
   public string subSndValue[RBT_TONE_ID_LEN];
   public string subMsgId;
   public byte subBillFlag;
   public string subGroupOrder;
   public string subDate[9];
   public string subGroupName[31];
   public byte subSpFlag;
   public string subBillCode[21];
   public byte subReserve[30];
   public int bodyLength;
   public SKT_body_t bodyData[1]; 

   public SKT_Subheader_s()
   {
      //Constructor, Initialize your variables here
      //e.g.
      // sublength = 0;
      //etc...
   }
}

Upvotes: 4

LiuYan 刘研
LiuYan 刘研

Reputation: 1624

Maybe you can simply use a byte[] array to store this structured data, and use java.nio.ByteBuffer to wrap and manipulate it.

Upvotes: 1

uckelman
uckelman

Reputation: 26224

What you need is a Java class with public members.

Upvotes: 0

MarcoS
MarcoS

Reputation: 13564

Java has no equivalent of struct. You must create a class to represent your struct. A good approach is to create a Java Bean. Your struct elements become private fields, and then you create getter (and setter) methods for accessing (modifying) them. You should also create an appropriate constructor for your class, depending how you want to initialize an object of such a class.

A note on primitive data types: I'm not 100% sure what U32, U8, S16, etc. represent in your code, but you should have a look at Java Primitive Data Types to be sure that you can map them correctly. For example, be aware that Java does not implement unsigned ints (see here).

Upvotes: 1

Argote
Argote

Reputation: 2155

It seems to me like you just need to create a class which has those datatypes as public variables. You may want to initialize the variables in the constructor as well.

Also, it is more common in Java to have the variables in a class like this set to private and just exposing getters and setters publicly.

Upvotes: 3

Related Questions