Solved

Unix byte order

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
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.
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 …
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

707 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

15 Experts available now in Live!

Get 1:1 Help Now