![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4
![]() |
well...but my question was...how do i do it myself
![]()
__________________
Spread your wings and fly! Chicken! |
|
|
|
|
|
#4 |
|
Professional Programmer
|
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 *) #
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;
} |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4
![]() |
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;
}
__________________
Spread your wings and fly! Chicken! |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#9 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5
![]() |
Quote:
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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|