473,322 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 8574
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

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

Similar topics

3
by: Vumani Dlamini | last post by:
I have data which is in this format: ### data ### area=1101 home=003 mzer=00020 mzec=101 pmpr=00000 pmpc=102 bnsr=00000 bnsc=103
1
by: Giovanni Azua | last post by:
Hello all, I need to return back to TSQL two numeric values from an Extended Stored Procedure developed in C. As result my C dll program produces two float values, the TSQL side expects to have...
1
by: Majed | last post by:
hi, I'm trying to convert some .h files to API Declaration to use the function and structs. one of it is this: NTMS_GUID CurrentLibrary; // the current library NTMS_GUID MediaPool; // media...
2
by: Con | last post by:
Hi, I'm in the process of converting some PERL to PHP. Thus, is it possible to leave the following line as is: if ($id =~ /vvvvv/g) { ... } If not, can someone provide PHP equivalent? ...
4
by: eight02645999 | last post by:
hi i need help with converting a piece of perl code to python ...... my $start = '\'; my $file = '\'; my $end = '\'; ..... while(<FILE>) #open a file {
7
by: Angus Comber | last post by:
Hello Here is my code so far. Is this correct/incorrect/along the right lines/other? #include <stdio.h> #include <string.h> #include <search.h> struct mystruct {
1
by: DaTurk | last post by:
I'm having an issue converting an unmanaged struct to a CLI managed struct. It's a value struct and this cannot be changed. public value struct UpdateItem { public: System::String ...
2
by: lbscprogrammer | last post by:
Hi, I'm in trouble here... I need to get this program working to proceed with my application. The program is very complex, it works fine on a 10 year old system written in IBM C/2. I am showing...
4
by: screamer81 | last post by:
Hello, I've a SDK functions description for a scanner and I try to convert unmanaged DLL C++ functions to c#. I converted all except one of them. This function is getting a struct parameter. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.