Do you mean an 8 byte integer to begin with? Since your intermediate layer is a Perl CGI, typecasting per se is not an option. But, you could either simply ensure that the value you wish to pass to the second C program doesn't exceed the capacity of an unsigned long (4 byte int), or use pack/unpack with the L option. Take a look at perlpacktut, specifically the "Integers" section.

Update: I believe in this case you'll need to pack the number into a binary format to begin with, and then unpack it before sending it off again. In your example unpack is assuming that the value supplied it is binary, and then doing some wierdness to give you the result you see. Instead, try this:

my $orig = 256000000; my $temp = pack('L', $orig); my $result = unpack('L', $temp);

Yields 256000000, which is what we want because that fits into a long. In contrast,

my $orig = 256000000000; my $temp = pack('L', $orig); my $result = unpack('L', $temp);

yields 4294967295 which is correct as 2**32 == 4294967296, the max storage of a long (according to Perl). Be sure to pay attention to the caveats in perlpacktut regarding the looseness of the C standard regarding the exact size of these types, making the exact conversion you want in this case dependant upon the compiler used in your C applications. HTH.


In reply to Re: casting to a 4 byte integer? by djantzen
in thread casting to a 4 byte integer? by cyberconte

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.