?
Solved

Unix byte order

Posted on 1998-11-04
4
Medium Priority
?
186 Views
Last Modified: 2013-11-13
I am writing a small TCP/IP client using the Winsock control.  The client is designed to connect to a particular server which expects a special sequence of bytes during an initial handshake.  I need to send four bytes in "Unix byte order".(In particular, I need to send a four byte sequence containing the number of characters in the string that I am about to send (which is 8)).

The server documentation mentions a C/C++ macro called "htonl" that can be used in those languages.  I'm looking to accomplish the same thing in Visual Basic.

I have a rudimentary understanding of the concepts, but I have no idea how to go about implementing them.

Thank you in advance.
0
Comment
Question by:krouk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 12

Accepted Solution

by:
mark2150 earned 800 total points
ID: 1443165
Some systems store data "big endian" where the MS Byte of data is the lower addressed byte. Intel stores data "little endian" where the LS byte of data is the lower addressed byte. This leads to no end of confusion. When you do a bytewise memory dump the addresses normally increase reading left to right. The data values are LS/MS order and you're forever having to swap halves. When you send to the UNIX system you'll have to take this byte ordering problem into account. Normally you'll send the four bytes in MSB/2MS/2LS/LSB order. These will be stored LSB/2LS/2MS/MSB order on an intel box.

M

0
 
LVL 12

Expert Comment

by:mark2150
ID: 1443166
As a learning experience go to the DOS prompt and do the following (ignore the parenthetical comments):

C:\> DEBUG                   (Start DEBUG from DOS prompt)
-f 0,ffff 0                           (fill memory with 0's)
-a 100                             (Assemble at 100H)
xxxx:0100 dw 1234 5678  (Define two words, '1234' and '5678')
xxxx:0104                       (enter on a line by itself to stop assembling)
-d 100                             (Display memory at 100H)
xxxx:0100  34 12 78 56 00 00 00 00-00 00 00 00 00 00 00 00   4.xV............
.

(Note the order that the data was saved in: 34 12 78 56, the MS of each word is at the lower memory address.)

-q                                   (exit debug back to DOS)

M


0
 

Author Comment

by:krouk
ID: 1443167
Thank you for your comments.  They are very helpful.  You will definitely get an "A" for this.  But, I would like to confirm my understanding by giving an example of how I might implement a conversion from intel to unix byte order.

Based on what you said, would I be able to do something like this?

Dim thebytearray () as byte
Dim items as integer
Dim first as byte
Dim second as byte

Thebytearray = "8"

For items = 0 to Ubound (thebytearray) step 2
  first = thebytearray (items)
  second = thebytearray (items + 1)
  thebytearray (items) = second
  thebytearray (items + 1) = first
Next

Thanks again for your time.  Please note that I will be out for the next couple of days, so I may not have a chance to award your points until next week (so there is no rush on your reply).
0
 
LVL 12

Expert Comment

by:mark2150
ID: 1443168
Your example (after a cursory reading) looks ok, but you might want to simplify it:
Dim bytearray(8) as byte
call sendabyte( bytearray( 2 ) )
call sendabyte( bytearray( 1 ) )
call sendabyte( bytearray( 4 ) )
call sendabyte( bytearray( 3 ) )
call sendabyte( bytearray( 6 ) )
call sendabyte( bytearray( 5 ) )
call sendabyte( bytearray( 8 ) )
call sendabyte( bytearray( 7 ) )

This is (perhaps) a little clearer if not as efficient.

M
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

764 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