Gamestudio Links
Zorro Links
Newest Posts
loading historical data 1st time
by AndrewAMD. 04/14/23 12:54
Trade at bar open
by juanex. 04/13/23 19:43
Bug in Highpass2 filter
by rki. 04/13/23 09:54
Adding Limit Orders For IB
by scatters. 04/11/23 16:16
FisherN
by rki. 04/11/23 08:38
AUM Magazine
Latest Screens
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Hecknex World
Who's Online Now
3 registered members (AndrewAMD, The_Judge, Grant), 898 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
rki, FranzIII, indonesiae, The_Judge, storrealba
18919 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
little endian -> big endian #185195
02/23/08 02:57
02/23/08 02:57
Joined: May 2002
Posts: 7,441
ventilator Offline OP
Senior Expert
ventilator  Offline OP
Senior Expert

Joined: May 2002
Posts: 7,441
i don't know if this is the best way to convert from little endian to big endian but i came up with this function:
Code:
long le2be(long *i)
{
long l =
((*i & 0x000000FF) << 24) +
((*i & 0x0000FF00) << 8) +
((*i & 0x00FF0000) >> 8) +
((*i & 0xFF000000) >> 24);
return l;
}



in lite-c this function doesn't work reliably. i write out vertex positions and a part of the floats i feed into it come out wrongly. many work though.

then i created a dll with exactly the same function compiled with visualc++ and everything worked correctly.

why?

Re: little endian -> big endian [Re: ventilator] #185196
02/23/08 15:38
02/23/08 15:38
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
be aware of signed integers... they often don't work with bitwise operators.

Code:
unsigned long le2be(unsigned long* le_32)
{
unsigned char* le_8 = (unsigned char*)le_32;
return ((unsigned long)le_8[3] << 24) |
((unsigned long)le_8[2] << 16) |
((unsigned long)le_8[1] << 8) |
(unsigned long)le_8[0];
}



that's how i'd have done it. in c++ i'd have used a reinterpret_cast for the pointer conversion and a static_cast for the integer conversions.

Re: little endian -> big endian [Re: Joey] #185197
02/23/08 15:42
02/23/08 15:42
Joined: May 2002
Posts: 7,441
ventilator Offline OP
Senior Expert
ventilator  Offline OP
Senior Expert

Joined: May 2002
Posts: 7,441
yes, unsigned would make sense but i didn't use it because lite-c doesn't support it anyway. though the strange thing is that exactly the same code works with visual-c++.

Re: little endian -> big endian [Re: ventilator] #185198
02/23/08 18:18
02/23/08 18:18
Joined: Jan 2004
Posts: 2,013
The Netherlands
E
Excessus Offline
Expert
Excessus  Offline
Expert
E

Joined: Jan 2004
Posts: 2,013
The Netherlands
Yes signedness was my first thought too, but didn't post because both Lite-C and VC++ default to signed when it is not specified.

Maybe you can try using | instead of +, because | works bitwise and doesn't care about signs (I think).

Re: little endian -> big endian [Re: Excessus] #185199
02/23/08 18:28
02/23/08 18:28
Joined: May 2002
Posts: 7,441
ventilator Offline OP
Senior Expert
ventilator  Offline OP
Senior Expert

Joined: May 2002
Posts: 7,441
i tried joey's version but it didn't work with lite-c. it seemed to cause even worse results.

is doing this possible at all with lite-c? since exactly the same code works with visual-c++ it could be some lite-c problem i guess?

Re: little endian -> big endian [Re: ventilator] #185200
02/24/08 02:40
02/24/08 02:40
Joined: Oct 2003
Posts: 702
Z
zazang Offline
User
zazang  Offline
User
Z

Joined: Oct 2003
Posts: 702
maybe it has something to do with how many bytes the data type takes in lite-c
and vc++ ?


I like good 'views' because they have no 'strings' attached..
Re: little endian -> big endian [Re: Joey] #185201
03/03/08 12:44
03/03/08 12:44
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
another try which might work in lite-c:

Code:
long le2be(long le_32)
{
char b1 = le_32 >> 24, b2 = le_32 >> 16, b3 = le_32 >> 8, b4 = le_32;
return (((long)b4) << 24) |
(((long)b3) << 16) |
(((long)b2) << 8) |
(long)b1;
}



i still don't really know how the lite-c compiler treats signed integers. i wouldn't do that in standard c since there's no such thing as a reinterpret_cast. if the compiler tries to convert between the different variable types... dunno what'll happen then. how about:

Code:
xor edx, edx

mov eax, le_32
shr eax, 18h
or edx, eax

mov eax, le_32
shr eax, 08h
and eax, ff00h
or edx, eax

mov eax, le_32
shl eax, 08h
and eax, ff0000h
or edx, eax

mov eax, le_32
shl eax, 18h
or edx, eax

mov le_32, edx



Last edited by Joey; 03/03/08 12:57.
Re: little endian -> big endian [Re: Joey] #185202
03/03/08 18:04
03/03/08 18:04
Joined: May 2002
Posts: 7,441
ventilator Offline OP
Senior Expert
ventilator  Offline OP
Senior Expert

Joined: May 2002
Posts: 7,441
this one works in lite-c:

Code:
long le2be(long *i)
{
return
((*i << 24) & 0xFF000000) +
((*i << 8) & 0x00FF0000) +
((*i >> 8) & 0x0000FF00) +
((*i >> 24) & 0x000000FF);
}




Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1