469,315 Members | 1,795 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

Converting C struct to Perl

158 100+
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.
May 31 '07 #1
14 8307
kardon33
158 100+
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?

Expand|Select|Wrap|Line Numbers
  1. #ifndef _ARTNET_H_
  2. #define _ARTNET_H_
Expand|Select|Wrap|Line Numbers
  1. #define uchar unsigned char
  2. #define ushort unsigned short int
  3. #define ulong unsigned int
May 31 '07 #2
miller
1,089 Expert 1GB
Give us the full command or piece of code that you're trying to convert.

- Miller
May 31 '07 #3
RedSon
5,000 Expert 4TB
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?

Expand|Select|Wrap|Line Numbers
  1. #ifndef _ARTNET_H_
  2. #define _ARTNET_H_
Expand|Select|Wrap|Line Numbers
  1. #define uchar unsigned char
  2. #define ushort unsigned short int
  3. #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.
May 31 '07 #4
kardon33
158 100+
ok thanks that helps alot.
May 31 '07 #5
RedSon
5,000 Expert 4TB
Merge me !
May 31 '07 #6
kardon33
158 100+
Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2.     uchar IP[4];    // IP address
  3.     ushort Port;    // UDP port    BYTE-SWAPPED MANUALLY
  4. } T_Addr;
May 31 '07 #7
kardon33
158 100+
Expand|Select|Wrap|Line Numbers
  1. #define uchar unsigned char
  2. #define ushort unsigned short int
  3. #define ulong unsigned int
  4.  
  5. typedef struct S_ArtDmx {
  6.     uchar ID[8];                
  7.     ushort OpCode;                  
  8.     uchar VersionH;               
  9.     uchar Version;                  
  10.     uchar Sequence;            
  11.     uchar Physical;               
  12.     ushort Universe;             
  13.     ushort Length;            
  14.     uchar Data[MaxDataLength];
  15. } T_ArtDmx;
Could some one help me convert that to PERL?
May 31 '07 #8
miller
1,089 Expert 1GB
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
Jun 1 '07 #9
kardon33
158 100+
Ok thank you that helps, but one question what is the diffrence between hash and pack/unpack
Jun 1 '07 #10
KevinADC
4,059 Expert 2GB
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.
Jun 1 '07 #11
kardon33
158 100+
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?
Jun 1 '07 #12
kardon33
158 100+
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.
Expand|Select|Wrap|Line Numbers
  1. #define uchar unsigned char
  2. #define ushort unsigned short int
  3. #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,
Expand|Select|Wrap|Line Numbers
  1. $uchar = "2131Adam2113";
  2. pack 'C', $uchar;
  3. print $uchar;
But for some reason i get the output of 2131Adam2113 instead of just char like Adam???
Jun 1 '07 #13
miller
1,089 Expert 1GB
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
Jun 1 '07 #14
kardon33
158 100+
thanks i actually just found that out before u replied.
Jun 1 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Vumani Dlamini | last post: by
2 posts views Thread by Con | last post: by
4 posts views Thread by eight02645999 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.