James Turnbull wrote:
> Hi

Hello,

> I have a C data structure being outputted to a Unix socket that I need
> to convert into a form Perl can use.  I am using IO::Socket to connect
> to the socket and I can receive the data fine.
> 
> I am using the Perl unpack function to convert the data.  I've read the
> documentation and all the examples and now I just need some help mapping
> data types between C and Perl.  My C data structure looks like:
> 
> u_int8_t alert
> u_int32_t var1
> u_int32_t var2
> u_int32_t var3
> u_int32_t var4
> u_int8_t var5
> u_int32_t var6
> bpf_u_int32 var7
> bpf_u_int32 var8
> 
> My template looks like:
> 
> $template = "C L4 C L ? ?"
> 
> Unfortunately the data unpacked does not match the expected data.  Have
> I got the right data type conversations taking place?
> 
> BTW ignore the ?'s they indicate the bpf_u_int32 data type as I don't
> know what Perl data type to put here.  I have been excluding these for
> now. Anyone know what data type to use?
> 
> Can anyone provide any hints about how I should do this?

The C programming language has four basic integer types: char, short, int and
long so your integer types are probably defined in a header file somewhere.
Although the numbers in the types probably determine how many bits each
integer contains.

Structures in C are usually padded so that integers that are smaller then the
native CPU integer size occupy the same space as the native integer size in
the structure and of course some compilers allow you to turn off padding.  So
the type u_int8_t may actually occupy 32 bits in the structure.

Different CPUs store the octets of integers in different order.  For example,
Intel compatible CPUs use a little-endian format while some IBM and Motorola
CPUs use a big-endian format.

So to determine the correct unpack() format you first have to determine the
endianess of the sending computer and whether the structure is padded or not
and if so how much padding is used.



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to