Author |
Message |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Fri Jun 25, 2004 5:16 pm Post maybe stupid Post subject: |
 |
|
|
|
OMG! That is the single most inefficient way to do it. You really couldn't make it any worse without Qndre's help...
void SwapEndian (void *pData, int iBytes)
{
char *p = (char *)pData;
char *q = p + iBytes - 1;
// don't even check for odd
while (iBytes--)
*p++ = *q--;
} |
If you want to be even more efficient, write SWAP2(), SWAP4(), SWAP8() macros that do it all inline...
#define SWAP2(x) ((x >> 8) | ((x & 0xff) << 8))
etc |
_________________ 4,691 irradiated haggis! |
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
Posted: Fri Jun 25, 2004 5:36 pm Post maybe stupid Post subject: |
 |
|
|
|
Umm wait is it just me or will that start overwriting data in pData as it copies? There has to be a temporary buffer or you get data loss.
And that's not the most inefficient way to do it, take a look at this code someone else wrote:
inline void SwapEndian64(char *pData)
{
char cTemp;
cTemp = pData[0];
pData[0] = pData[7];
pData[7] = cTemp;
cTemp = pData[1];
pData[1] = pData[6];
pData[6] = cTemp;
cTemp = pData[2];
pData[2] = pData[5];
pData[5] = cTemp;
cTemp = pData[3];
pData[3] = pData[4];
pData[4] = cTemp;
} |
Edit: Yep, just tested it.
void SwapEndian (void *pData, int iBytes)
{
char *p = (char *)pData;
char *q = p + iBytes - 1;
// don't even check for odd
while (iBytes--)
*p++ = *q--;
}
int main(int argv, char **argc)
{
char test[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
SwapEndian(test, 8);
return 0;
} |
In the memory debug window, test ended up being: 08 07 06 05 05 06 07 08. Cool little relfection thingy though  |
|
Back to top |
|
 |
Cyan~Fire I'll count you!

Age:37 Gender: Joined: Jul 14 2003 Posts: 4608 Location: A Dream Offline
|
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
|
Back to top |
|
 |
SuSE Me measures good

Joined: Dec 02 2002 Posts: 2307 Offline
|
|
Back to top |
|
 |
Dustpuppy Server Help Squatter

Age:40 Gender: Joined: Jan 23 2003 Posts: 215 Location: England Offline
|
|
Back to top |
|
 |
SuSE Me measures good

Joined: Dec 02 2002 Posts: 2307 Offline
|
Posted: Fri Jun 25, 2004 7:57 pm Post maybe stupid Post subject: |
 |
|
|
|
LIES!! I HAVE CODED A PROGRAM IN ASM THAT WILL EXTRACT YOUR BRAIN!! |
|
Back to top |
|
 |
CypherJF I gargle nitroglycerin

Gender: Joined: Aug 14 2003 Posts: 2582 Location: USA Offline
|
Posted: Fri Jun 25, 2004 8:35 pm Post maybe stupid Post subject: |
 |
|
|
|
*confused*  _________________ Performance is often the art of cheating carefully. - James Gosling |
|
Back to top |
|
 |
D1st0rt Miss Directed Wannabe

Age:37 Gender: Joined: Aug 31 2003 Posts: 2247 Location: Blacksburg, VA Offline
|
Posted: Sat Jun 26, 2004 12:49 am Post maybe stupid Post subject: |
 |
|
|
|
Ekted lol, I was at a bookstore and I saw a book you could've written. "How NOT to program in C++ (119 Programs that don't work)" _________________
 |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Sat Jun 26, 2004 1:02 am Post maybe stupid Post subject: |
 |
|
|
|
LOL! |
|
Back to top |
|
 |
Dustpuppy Server Help Squatter

Age:40 Gender: Joined: Jan 23 2003 Posts: 215 Location: England Offline
|
Posted: Sat Jun 26, 2004 1:47 pm Post maybe stupid Post subject: |
 |
|
|
|
SuSE wrote: | LIES!! I HAVE CODED A PROGRAM IN ASM THAT WILL EXTRACT YOUR BRAIN!! |
Pfft, you can't extract my brain. It's tarballed and encrypted using an algorithm designed by Qndre himself. |
|
Back to top |
|
 |
Mr Ekted Movie Geek

Gender: Joined: Feb 09 2004 Posts: 1379 Offline
|
Posted: Sat Jun 26, 2004 2:46 pm Post maybe stupid Post subject: |
 |
|
|
|
The one with the 536,189 bit key? It's hopeless... |
|
Back to top |
|
 |
Dustpuppy Server Help Squatter

Age:40 Gender: Joined: Jan 23 2003 Posts: 215 Location: England Offline
|
Posted: Sat Jun 26, 2004 5:36 pm Post maybe stupid Post subject: |
 |
|
|
|
Mr Ekted wrote: | The one with the 536,189 bit key? It's hopeless... |
What? But it has a 536,189 bit key. 536,189 BITS!!!!!!11111111111111
That's like, 50 bytes or something11111111111111 |
|
Back to top |
|
 |
SuSE Me measures good

Joined: Dec 02 2002 Posts: 2307 Offline
|
Posted: Sun Jun 27, 2004 8:02 am Post maybe stupid Post subject: |
 |
|
|
|
lol, god bless qndre  |
|
Back to top |
|
 |
|