in reply to How to easily convert the pack function from little endian to big endian
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):
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:#!/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;
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.my $S16bits = substr($new, 0, 4, "" ); ## 4 bytes printable hex or my $S16bits = substr($ndata, 0, 2, "" ); ## 2 bytes hex data
Good Luck
"Well done is better than well said." - Benjamin Franklin
|
|---|