Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I convert Big-endian to Little-endian efficiently? 2

Status
Not open for further replies.

JLMartin

Programmer
Oct 26, 1999
1
US
I have a several Long numbers that are stored in Big-endian and I want to convert then Little-endian. How can I do this efficiently?
 
Hi JLMartin!<br>
<br>
Hmmm. Two methods come to mind:<br>
<br>
1) Call the Winsock function htonl() or ntohl() depending on the direction you want to go<br>
<br>
2) Do something ugly (but removes dependency on Winsock) like:<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp; Dim s1, s2, s3, s4 as byte<br>
&nbsp;&nbsp;&nbsp;&nbsp; s1 = (Num And &HFF000000) / &H01000000<br>
&nbsp;&nbsp;&nbsp;&nbsp; s2 = (Num And &H00FF0000) / &H00010000<br>
&nbsp;&nbsp;&nbsp;&nbsp; s3 = (Num And &H0000FF00) / &H00000100<br>
&nbsp;&nbsp;&nbsp;&nbsp; s4 = Num And &H000000FF<br>
&nbsp;&nbsp;&nbsp;&nbsp; Num = (s4 * &H00100000) + (s3 * &H00010000) + (s2 * &H00000100) + s1<br>
<br>
NOTE: I haven't tested this code - you might need to play around with the AND statements, but it looks good.<br>
<br>
Chip H.<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top