Solved

Converting from Little Endian to Big Endian

Posted on 2001-06-05
3
1,363 Views
Last Modified: 2012-06-27
Hi - I am opening a socket up from a Mandrake machine (Little Endian) to an HP9000 (Big Endian) and receiving a record out of a KISAM file into a variable.

I then write this variable to disk. KISAM files have a data dictionary which states information about where in the record a field is located and the length it is in bytes.

The data dictionary states that the field I am looking for is binary, starts at byte 2 and is 3 bytes long. The field when unpacked should be a long integer 176385.

If I write the variable containing the record I received to disk and open it in binary editor I get this (just showing the first four bytes):
00000000 00000010 10110001 00000001

I wrote a script in perl to pack 176385 and wrote the resulting variable to disk
$num = pack("L", 176385);
Editing this file in binary editor showed:
00000001 10110001 00000010

I presume that I need to convert my variable from big endian to little endian, do a substr($record,2,3)  to get the 3 bytes from start byte 2 (as the data dictionary states) and then unpack the resulting string.

How do I convert my variable from big endian to little endian in perl?

PS Im not being tight - I only have 75 points to offer!
0
Comment
Question by:kccsf
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 75 total points
ID: 6158320
$little = unpack"V",pack"N",$big;
0
 
LVL 5

Expert Comment

by:Sapa
ID: 6158924
kccsf:

 or even better, send all binary data in machine independent 'network order'.

$num=pack('N',176385);  # on sender side

and

$u32=unpack('N',$num); # on receiver side

- Sapa
0
 

Author Comment

by:kccsf
ID: 6208730
Apologies for delay - just got back rfom holiday and relooked at this.

The answer was to do this but I had to append a blank byte on the front of the substring if it was made up of an odd number of bytes.

Instead I am now looping through the number one byte at a time and bitshifting it depending on which byte it is ie:

1st byte
2nd byte <<8
3rd byte <<16

and totalling them up.

Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Strange perl issue 6 126
work on ods spreadsheet with perl in ubuntu 4 75
problem with using the glob function 1 63
Add additional column to .csv using Perl. 8 128
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now