Welcome semiconPERL,

I'm going to guess that your problem is more about transferring data from big to little endian and vice-versa. One method I use is to convert the data to printable hex which is in network order. See the below code (tested):

#!/usr/local/bin/perl -w my $data = ""; for (0..255) { $data .= chr($_); } ## All possible byte combinations open ( my $out, ">", "./testdata" ) or die "$!\n"; my $hdata = unpack("H*",$data); print $out "$hdata\n"; close $out; open ( my $in, "<", "./testdata" ) or die "$!\n"; my $sz = read( $in, my $new, 256 ); chomp( $new ); close $in; my $ndata = pack("H*",$new); # print "$hdata\n\n$new\n\n"; if ( $ndata eq $data ) { print "1. Okay\n"; } if ( $hdata eq $new ) { print "2. Okay\n"; } exit; 1;
I verified on both little/bin endian machines that the file created is exactly the same. You can ftp the files back and forth and all information will be in network order. You can process the data in 4 characters (16 bits), 8 characters (32 bits), or 16 characters (64 bits) by using the 'substr' function on the the beginning of the data:
my $S16bits = substr($new, 0, 4, "" ); ## 4 bytes printable hex or my $S16bits = substr($ndata, 0, 2, "" ); ## 2 bytes hex data
If you need to have variable data, then use a separator between variables in the data. I usually use 'chr(254)' as a separator since my editor shows it as a special character, but you can use any non-printable character. Then use 'split' to separate the variables.

Good Luck

"Well done is better than well said." - Benjamin Franklin


In reply to Re: How to easily convert the pack function from little endian to big endian by flexvault
in thread How to easily convert the pack function from little endian to big endian by semiconPERL

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.