Solved

Unix byte order

Posted on 1998-11-04
4
181 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
  • 3
4 Comments
 
LVL 12

Accepted Solution

by:
mark2150 earned 200 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

777 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