473,387 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Translate bit order

I am in need of a utility function to change host to network byte
order and back. If I understand things correctly, I just need to make
the most significant bit the least and vice versa or in essence write
the bits backwards.

Its been awhile since I played with bitwise operations. Is there a
better solution, without linking to winsock or a bsd library (just
standard C++), than:

ReverseBitOrder(unsigned long)
{
unsigned long newlong = 0;

// for each bit position in the long
// zero out all other bit's positions
// shift it to 32 - (the bit's position from least significant)
// add it to newlong;

return newlong;
}

Hell, I am not even sure if that is right.

Mar 9 '07 #1
4 2748
brekehan wrote:
I am in need of a utility function to change host to network byte
order and back. If I understand things correctly, I just need to make
the most significant bit the least and vice versa or in essence write
the bits backwards.
I am not 100% sure if that is correct.
>
Its been awhile since I played with bitwise operations. Is there a
better solution, without linking to winsock or a bsd library (just
standard C++), than:
If you are on Linux/Unix, you can use these:

NAME
htonl, htons, ntohl, ntohs - convert values between host and
network
byte order

SYNOPSIS
#include <arpa/inet.h>

uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);

Then you can test how the bits are massaged. Then you can attempt to
write your own version for portability or look for one on the Windows
side.

HTH!
Mar 9 '07 #2
brekehan wrote:
I am in need of a utility function to change host to network byte
order and back. If I understand things correctly, I just need to make
the most significant bit the least and vice versa or in essence write
the bits backwards.
That's not right. It's the bytes within a word that you need to reverse
(what a word is depends on the application, usually it's two or four bytes).

That's why it's called netword byte order, not network bit order.
>
Its been awhile since I played with bitwise operations. Is there a
better solution, without linking to winsock or a bsd library (just
standard C++), than:
Well, I guess you're glad to learn that you don't need to.

john
Mar 9 '07 #3
On Mar 9, 4:25 pm, John Harrison <john_androni...@hotmail.comwrote:
brekehan wrote:
I am in need of a utility function to change host to network byte
order and back. If I understand things correctly, I just need to make
the most significant bit the least and vice versa or in essence write
the bits backwards.

That's not right. It's the bytes within a word that you need to reverse
(what a word is depends on the application, usually it's two or four bytes).

That's why it's called netword byte order, not network bit order.
Its been awhile since I played with bitwise operations. Is there a
better solution, without linking to winsock or a bsd library (just
standard C++), than:

Well, I guess you're glad to learn that you don't need to.

john
Doh, thats what I orignally thought until someone wrote it on paper
with the bits reversed instead of bytes. I was wondering why I didn't
have to convert chars but I had to convert ints...

Mar 9 '07 #4
On Mar 9, 4:06 pm, Piyo <cybermax...@yahoo.comwrote:
brekehan wrote:
I am in need of a utility function to change host to network byte
order and back. If I understand things correctly, I just need to make
the most significant bit the least and vice versa or in essence write
the bits backwards.

I am not 100% sure if that is correct.
Its been awhile since I played with bitwise operations. Is there a
better solution, without linking to winsock or a bsd library (just
standard C++), than:

If you are on Linux/Unix, you can use these:

NAME
htonl, htons, ntohl, ntohs - convert values between host and
network
byte order

SYNOPSIS
#include <arpa/inet.h>

uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);

Then you can test how the bits are massaged. Then you can attempt to
write your own version for portability or look for one on the Windows
side.

HTH!
Yea, linux has a function for it and windows has a function for it if
using winsock. But I gotta write my own as I am working on a modular
layer that can easily be plugged into anything underneath. All kinds
of reinventions are coming up.

Mar 9 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: inquirydog | last post by:
Hello- I, the inquirydog, would like to solicit suggestions for a new web page I am making: I am creating a simple website that will translate concepts between windows os's, Linux, and the...
1
by: shank | last post by:
I'm sure this is a stretch, but is there some kind of component that I could install to translate from English to Spanish on the fly? I have a lot of equipment features and specifications that I...
6
by: Gancy | last post by:
Hi all, Are there any version of g++ that translates C++ to C. or any open source compiler to do this. Thanks Ganesh
0
by: SimpleSimple | last post by:
My company has an IIS 5, ASP.net intranet site that contains documents for employee use. The files are of various types (most often Office) and are stored in blob fields in a SQL2000 database. ...
10
by: JohnR | last post by:
I have a datatable as the datasource to a datagrid. The datagrid has a datagridtablestyle defined. I use the datagridtablestyle to change the order of the columns (so they can be different than...
6
by: bobueland | last post by:
The module string has a function called translate. I tried to find the source code for that function. In: C:\Python24\Lib there is one file called string.py I open it and it says
1
by: rguarnieri | last post by:
I have this query in access and I need to translate to SQL ansi TRANSFORM Sum(BilletSCHQTY) AS SumOfBilletSCHQTY SELECT Tap, Mandrin, BilletOD, BilletLength, FROM Tbl_ExtruOptimize_Src GROUP...
9
bvdet
by: bvdet | last post by:
I have done some more work on a simple class I wrote to calculate a global coordinate in 3D given a local coordinate: ## Basis3D.py Version 1.02 (module macrolib.Basis3D) ## Copyright (c) 2006...
0
by: Studlyami | last post by:
I am trying to find how windows translate the input from a HID device to an actual windows message; specifically, the mouse. I am trying to figure out how when the mouse moves the process windows...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.