Solved

Unix byte order

Posted on 1998-11-04
4
183 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

713 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