Converting C struct to Perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kardon33
    New Member
    • May 2007
    • 158

    Converting C struct to Perl

    Hi i didnt know to post this into the C threads or Perl but what i need is to convert the comand: (typedef struct {}) in C to somthing that does the same thing in Perl.
  • kardon33
    New Member
    • May 2007
    • 158

    #2
    Converting

    I have 2 pieces of code that i do not know what it means. Could someone please explain what it is saying and if anyone could could you show me a way to do it in PERL?

    Code:
    #ifndef _ARTNET_H_
    #define _ARTNET_H_
    Code:
    #define uchar unsigned char
    #define ushort unsigned short int
    #define ulong unsigned int

    Comment

    • miller
      Recognized Expert Top Contributor
      • Oct 2006
      • 1086

      #3
      Give us the full command or piece of code that you're trying to convert.

      - Miller

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by kardon33
        I have 2 pieces of code that i do not know what it means. Could someone please explain what it is saying and if anyone could could you show me a way to do it in PERL?

        Code:
        #ifndef _ARTNET_H_
        #define _ARTNET_H_
        Code:
        #define uchar unsigned char
        #define ushort unsigned short int
        #define ulong unsigned int
        The first two lines can be read as "if _ARTNET_H_ is not defined then define _ARTNET_H_"

        The second line can be read as "define uchar as a synonym to unsigned char" and "define ushort to be a synonym to unsigned short int" and "define ulong to be a synonym to unsigned int".

        As for a PERL interpretation you will have to ask the PERL forum. Or I can just move this post over there.

        Comment

        • kardon33
          New Member
          • May 2007
          • 158

          #5
          ok thanks that helps alot.

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            Merge me !

            Comment

            • kardon33
              New Member
              • May 2007
              • 158

              #7
              Code:
              typedef struct {
              	uchar IP[4];	// IP address
              	ushort Port;	// UDP port	BYTE-SWAPPED MANUALLY
              } T_Addr;

              Comment

              • kardon33
                New Member
                • May 2007
                • 158

                #8
                Perl to C

                Code:
                #define uchar unsigned char
                #define ushort unsigned short int
                #define ulong unsigned int
                
                typedef struct S_ArtDmx {
                	uchar ID[8];                
                	ushort OpCode;                  
                	uchar VersionH;               
                	uchar Version;                  
                	uchar Sequence;			
                	uchar Physical;               
                	ushort Universe;             
                	ushort Length;			
                	uchar Data[MaxDataLength];
                } T_ArtDmx;
                Could some one help me convert that to PERL?

                Comment

                • miller
                  Recognized Expert Top Contributor
                  • Oct 2006
                  • 1086

                  #9
                  If you're just creating is a struct, then the perl equivalent would be a hash. If you're sending this information over the net and need to receive it, then you're going to need pack and unpack.

                  - Miller

                  Comment

                  • kardon33
                    New Member
                    • May 2007
                    • 158

                    #10
                    Ok thank you that helps, but one question what is the diffrence between hash and pack/unpack

                    Comment

                    • KevinADC
                      Recognized Expert Specialist
                      • Jan 2007
                      • 4092

                      #11
                      Originally posted by kardon33
                      Ok thank you that helps, but one question what is the diffrence between hash and pack/unpack

                      a hash (in perl) is essentially an array, pack/unpack are functions.

                      Comment

                      • kardon33
                        New Member
                        • May 2007
                        • 158

                        #12
                        Originally posted by miller
                        If you're just creating is a struct, then the perl equivalent would be a hash. If you're sending this information over the net and need to receive it, then you're going to need pack and unpack.

                        - Miller
                        You say i need to use pack if i am going to be sending the info over the net, well could you explain to me that why i could not use hash over the net to send it?

                        Comment

                        • kardon33
                          New Member
                          • May 2007
                          • 158

                          #13
                          I think i have the idea to the pack and unpack functions i am using but it still is not working write. First here is the code i need to transfer to PERL.
                          Code:
                          #define uchar unsigned char
                          #define ushort unsigned short int
                          #define ulong unsigned int
                          Im at the understanding that is saying i need to define the varible uchar and the others as an unsigned char. In PERL i think i do that by using pack with the C operator so the code i have looks like,
                          Code:
                          $uchar = "2131Adam2113";
                          pack 'C', $uchar;
                          print $uchar;
                          But for some reason i get the output of 2131Adam2113 instead of just char like Adam???

                          Comment

                          • miller
                            Recognized Expert Top Contributor
                            • Oct 2006
                            • 1086

                            #14
                            A hash is an perl internal data structure. It's essentially just an associative array.

                            Whenever you send a C struct across the net, it often must be serialized in some way. And then the receiving end must decrypt the serialization and turn it back into a C struct.

                            pack and unpack are the functions that are most often used to serialize data in perl for network programming.

                            - Miller

                            Comment

                            • kardon33
                              New Member
                              • May 2007
                              • 158

                              #15
                              thanks i actually just found that out before u replied.

                              Comment

                              Working...