• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 805
  • Last Modified:

Big Endian Little endian conversion - perl

Is there a feature in perl that would do a byte swap?
0
jewee
Asked:
jewee
  • 7
  • 5
1 Solution
 
ozoCommented:
$x = 0x1234;
$y= unpack'n',pack'v',$x;
printf "%x\n",$y;
0
 
jeweeAuthor Commented:
I am getting data from a socket...i would need it in hex format..
0
 
ozoCommented:
Could you give an example of what is in your input data, and what do you want to produce as output data?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jeweeAuthor Commented:
i am receiving 40 bytes from a socket and storing it into a buffer.

Then I use unpack:

  my ($taskid, $size, $clientId, $status, $command, $driverError) = unpack "C4 C4 C4 C4 C4 C4", $buffer;

I need to swap bytes prior to this.
0
 
ozoCommented:
my ($taskid, $size, $clientId, $status, $command, $driverError) = unpack "C4 C4 C4 C4 C4 C4", $buffer;
will make $taskid, $size, $clientId, $status, $command, $driverError
equal to the unsigned values of the first six bytes in $buffer, discarding the next 18 extracted byte values.
Since this is probably not what you want, could you describe what bytes
you want from $buffer, and how you want them translated into $taskid, $size, $clientId, $status, $command, $driverError?
0
 
jeweeAuthor Commented:
actually, that was only a partial list....I have the first 24 bytes i am getting from the socket.

Then, the next 16 bytes, i retrieve after...C8 C8
0
 
ozoCommented:
 my ($taskid, $size, $clientId, $status, $command, $driverError) = unpack "C4 C4 C4 C4 C4 C4", $buffer;
Is the same as
  my ($taskid, $size, $clientId, $status, $command, $driverError) = unpack "C24", $buffer;
Which extracts 24 one byte values, of which you are only assigning the first 6,
so it has the same effect as
  my ($taskid, $size, $clientId, $status, $command, $driverError) = unpack "C6", $buffer;
or
  my ($taskid, $size, $clientId, $status, $command, $driverError) = unpack "CCCCCC", $buffer;
Do you want $taskid to be the unsigned value of the first byte in $buffer, which is what the above statement does?
Or were you expecting $taskid to be extracted from the first four bytes of $buffer?
If so, did you want $taskid to be a four byte string?
A four byte integer in big-endian format?
A four byte integer in little-endian format?
Something else?

It may help to know how $buffer was constructed.
0
 
jeweeAuthor Commented:
Here is a snippet of my code.  I need to convert the output from big endian to little endian format then have a 4 byte integer.

while($socket->connected) {
  $socket->write($requestMsg);
 
  #Retrieve only header
  $socket->read($buffer,24);
 
0
 
ozoCommented:
If $buffer containes packed big endian 4 byte unsigned integers, then you can unpack them with
  my ($taskid, $size, $clientId, $status, $command, $driverError) = unpack "N4 N4 N4 N4 N4 N4", $buffer;
If $buffer containes packed little endian 4 byte unsigned integers, then you can unpack them with
  my ($taskid, $size, $clientId, $status, $command, $driverError) = unpack "V4 V4 V4 V4 V4 V4", $buffer;

How was the message sent from the other end of the socket constucted?
0
 
ozoCommented:
Sorry, that should have been
  my ($taskid, $size, $clientId, $status, $command, $driverError) = unpack "N6", $buffer;
or
  my ($taskid, $size, $clientId, $status, $command, $driverError) = unpack "V6", $buffer;
0
 
jeweeAuthor Commented:
@requestBytes=( 0xD0, 0x00, 0x00, 0x00,
                0x28, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x65, 0x00, 0x00, 0x00,  
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x07, 0x00, 0x00, 0x00 );
               
#print "@requestBytes\n";

$requestMsg = pack 'C*',@requestBytes;

  $socket->write($requestMsg);

I thought I could unpack the same way except for the byte ordering.  I will try that.
0
 
ozoCommented:
You could
my @Bytes = unpack'C*',$requestMsg;
to produce
@Bytes=( 0xD0, 0x00, 0x00, 0x00,
                0x28, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x65, 0x00, 0x00, 0x00,  
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x07, 0x00, 0x00, 0x00 );
You could
my @Big = unpack 'N*',$requestMsg;
to produce
@Big=(0xd0000000,
           0x28000000,
           0x00000000,
           0x00000000,
           0x65000000,
           0x00000000,
           0x00000000,
           0x00000000,
           0x00000000,
           0x07000000,
);
or
my @Little = unpack'V*',$requestMsg;
to produce
@Little = (0x000000d0,
               0x00000028,
               0x00000000,
               0x00000000,
               0x00000065,
               0x00000000,
               0x00000000,
               0x00000000,
               0x00000000,
               0x00000007,
);

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now