Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 14th, 2005, 6:41 PM   #1
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
byte reversing & streaming

Well...HELP!!! Just have 2 questions

1. What are some of the efficient ways of playing around with byte ordering? e.g. changing 0xFFCCDDEE to 0xEEDDCCFF.

2. I've made a program that parses intercepted data between a server and a client program to model client/server state. For convenience I've made a custom class that buffers the recived data and parses them. Its sortof like the "StreamBuffer" class in Java where you go .nextInt() or whatever and it progresses pointer giving you the values etc... Well, the problem is, the byte ordering looks reversed! for things like ints! e.g. for an unsigned int, 4 = 0x00000004...but the data I get reads 0x04000000! Initially I thaught its to do with a bug with the interception method ive impimented. But It's all good. Because I dont get problems with strings byte ordering. My suspicion is that it uses one of c++'s "standard" streaming classes...which upon works putes bytes in that order! Or they are just the way things are put set into memory! I think its the former! So I just want to know what you think. Or which "standard" class the programmer might have used to do the buffering. (sorry...I am not vary good with default library classes...I just look up the platform sdk to custom do things my way).


Thanks in advance
__________________
Spread your wings and fly! Chicken!
rsnd is offline   Reply With Quote
Old Nov 14th, 2005, 7:39 PM   #2
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
There are the standard family of functions (for Berkeley sockets) like:

htons >> host to network short
htonl >> host to network long
ntohs >> network to host short
ntohl >> network to host long

etc.. for converting to/from network byte order, just add this to your buffer class.
Animatronic is offline   Reply With Quote
Old Nov 17th, 2005, 11:22 PM   #3
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
well...but my question was...how do i do it myself
__________________
Spread your wings and fly! Chicken!
rsnd is offline   Reply With Quote
Old Nov 18th, 2005, 12:25 AM   #4
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 310
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
This code may or may not help you - I'm not sure if it's exactly what you're looking for :p

#include <stdio.h>
 
int main();
unsigned long convert(long num);
 
int main()
{
        unsigned long lendian = 1234;
        unsigned long bendian = convert(lendian);
 
        printf("Value: %lu\n", lendian );
 
        printf("Converted: %lu\n", bendian);
 
        lendian = convert(bendian);
        printf("Converted back: %lu\n", lendian);
 
        return 0;
}
 
unsigned long convert(long num)
{
        long returnval;
        char *p_num  = (char *) &num;
        char *p_returnval = (char *) &returnval;
 
        int i, j;
        for (i = 0, j = 3; i <4; i++, j--)
        {
                p_returnval[i] = p_num[j];
        }
 
 
        return returnval;
}
andro is offline   Reply With Quote
Old Nov 18th, 2005, 4:43 AM   #5
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
The point is that you dont do the convertion yourself. Doing it yourself means you need to know the endianess of your system whilst using the standard functions you dont. Also I doubt your'll find a better implementation than the standard ones.
Animatronic is offline   Reply With Quote
Old Nov 18th, 2005, 6:24 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I'm not sure you realize that there IS an endianess issue. Not all machines arrange the individual bytes of a group in the same order. The common desktop Intel machines put the least significant byte first ("little endian"). Others put the msb first, in the way that we typically write our numbers (most western cultures). There are also in-between mixes. In the world of networking, one uses "network order." One calls the aforementioned functions as an interface. If no adjustment is necessary, your system's version will make none; if it is, it will.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 20th, 2005, 10:16 PM   #7
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
Thanks everyone I understand the issue now.
Anyways...will this work if I just add it to my partially working class?
	unsigned int GetUINT(){
		unsigned int temp=0;
		temp= (*(buffer+pointer++))<<0;
		temp|= (*(buffer+pointer++))<<8;
		temp|= (*(buffer+pointer++))<<16;
		temp|= (*(buffer+pointer++))<<24;
		return temp;
	}
have read access the the buffer.
__________________
Spread your wings and fly! Chicken!
rsnd is offline   Reply With Quote
Old Nov 21st, 2005, 6:37 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
If you know the underlying nature of your particular platform you can get away with coercions of various types. If you don't, you'll just make yourself trouble. That's why they make the functions. Your shifting of an entity by zero bits raises some question. If you want the practice, find out how your machine stores floats, then peel it apart and convert it yourself. If you're trying to conform to network byte order as a matter of course, use the standard functions.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 21st, 2005, 11:17 PM   #9
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by rsnd
unsigned int GetUINT(){
	unsigned int temp=0;
	temp= (*(buffer+pointer++))<<0;
	temp|= (*(buffer+pointer++))<<8;
	temp|= (*(buffer+pointer++))<<16;
	temp|= (*(buffer+pointer++))<<24;
	return temp;
}
This might work. Then again, it will not be as efficient as using the standard functions. On the Intel 80x86 CPUs from the 486 onwards, there is a BSWAP machine instruction that can do this quite quickly. I'd expect other modern CPUs to have a similar instruction. If you can use inline assembly, this might be an option for you. Note that BSWAP works on a 32-bit value; if you want to use it on a 16-bit value, you'll have to shift the register right 16 places.

Some example code follows.
; uses NASM syntax- you can adjust if needed
; for 32-bit values
mov eax, [value]
bswap eax
mov [value], eax
; for 16-bit values
mov ax, [value]
bswap eax
shr eax, 16
mov [value], ax
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC