1

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.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Abhisek
  • 715
  • 3
  • 13
  • 20
  • 1
    Please read http://stackoverflow.com/editing-help or have a look at the *How to Format* box to the right of the textarea to learn how to format code properly (just indent it with four spaces). No need for any HTML markup. – Felix Kling Apr 11 '11 at 10:03
  • What are those datatypes you're using? Will you have them on Java as well? – Argote Apr 11 '11 at 10:03
  • @felix I have tried by making a class but not able to succeed if you have any suggestion please give me. – Abhisek Apr 11 '11 at 10:06
  • Please edit your post adding the code you tried to use for the class as well as any compiler errors you may be getting. – Argote Apr 11 '11 at 10:07
  • @Argots that's why I have posted this q in this forum, no i don't know whether these data types are possible or not in java. If those data types are not possible in java then how to handle this problem? – Abhisek Apr 11 '11 at 10:09
  • @Argots no I am not getting any compiler error. The desired output is not coming , this is may be the reason as I am not able to create that proper structure like C code. – Abhisek Apr 11 '11 at 10:11
  • Still, it would be good if you posted what you have so far in Java. Those datatypes are not native to Java (nor are they native to C as far as I know, you probably are importing some library that defines them) – Argote Apr 11 '11 at 10:14

5 Answers5

4

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...
   }
}
Eamonn McEvoy
  • 8,876
  • 14
  • 53
  • 83
  • sorry this types of data types is not possible in java – Abhisek Apr 11 '11 at 10:15
  • @Abhisek When using a different language you sometimes need to choose an equivilant type native to that language, you could replace S8, S16 and S32 with string; and replace U8 and U16 with short, and U32 with unsigned int. http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html this site will help you choose suitable types. – Eamonn McEvoy Apr 11 '11 at 14:05
  • 1
    Why would you replace S8, S16 and S32 with string? Surely you'd replace them with byte, short and int? – Miles Rout Feb 18 '13 at 08:16
  • does that compile? – user666412 Jul 15 '16 at 22:26
3

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.

Argote
  • 2,155
  • 1
  • 15
  • 20
  • 1
    What Eamonn posted should get you started. however you'll need to decide which native Java datatype to use for `U32`, `U8` and so on as MarcoS mentioned. – Argote Apr 11 '11 at 10:16
1

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

LiuYan 刘研
  • 1,614
  • 1
  • 16
  • 29
  • I would like to know who and why down-vote my answer. If my answer isn't correct, please point out it, and I will correct it and I appreciate your help. If it's correct, please up-vote it! – LiuYan 刘研 Apr 11 '11 at 18:03
  • I can guess why you get a down-vote. Once you wrap your `byte[]` array with `ByteBuffer`, you will have additional overhead. This is my inexperienced guess, so please bear with me. – tom_mai78101 May 28 '14 at 07:16
0

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).

Community
  • 1
  • 1
MarcoS
  • 13,386
  • 7
  • 42
  • 63
0

What you need is a Java class with public members.

uckelman
  • 25,298
  • 8
  • 64
  • 82