Advertisement

byteswap

Started by August 17, 2001 12:30 PM
21 comments, last by Pillejunior 23 years, 6 months ago
I''m working on a communication between a pc and a plc. The only problem is the pc has a intel processor and the plc a motorola processor. I know that motorola processors switch the high byte and low byte from a word oppossed to a intel processor, but i do not know how to swap the high byte and low byte so that my value is recieved correctly by the plc from the pc. Does anyone has seen this problem before and if, how did yoy fix it. How can you swap bytes in a word and par example in a real which consist of 4 bytes. And how does motorola view a real in opposite to intel. thanx in advance
[edit, my brain was asleep]
WORD myNumber = ((myNumber << 8) | (myNumber >> 8);



Edited by - Torn Space on August 17, 2001 3:47:21 PM
Advertisement
Thanx torn, it''s fairly simply I see, but how about reals and dword''s. They have 4 bytes. How does motorola swap these and how do you do that in code. Can you just expand on the word example and take four steps instead of two
example
WORD myNumber =(myNumner & 0xFF000000)|(myNumber &0x00FF0000)|
(myNumber & 0x0000FF00)|(myNumber & 0x000000FF)
You''re talking about Big Endian and Little Endian processors. Intel is Little Endian, and I suppose motorola is Big Endian.

Winsock2 has functions that''ll switch for you. You can take a look:

ntohl - (network to host long)
ntohs - (network to host short)
htonl - (host to network long)
htons - (host to network short)

Each function takes in their respective types, and returns that type. Network order is Big Endian, where the sign bit is high. Host order is whatever your computer is. You should probably be using these functions. You can look these up, but the include file is Winsock2.h and the library is Ws2_32.lib.

BTW, I don''t think Torn Space''s function is correct (neither is yours). That''ll just give you the exact same number. It should be
something like:

WORD myNumber = ((myNumber << 8) | (myNumber >> 8);

DWORD myNumber = ((myNumber & 0xff000000) >> 24) |
((myNumber & 0x00ff0000) >> 8) |
((myNumber & 0x0000ff00) << 8) |
((myNumber & 0x000000ff) << 24);


Remember, you''re trying to SWAP the bytes.
Anyone else who knows a method to swap bytes in a word or dword or can tell me which method explained here is the right one or maybe both can. And does anyone know, just curieus, why motorola and intel have different ways of saving words and dwords in memory?
And thanx soulburn for your method
Pilejunior:

Soulburn''s algo is right, except you might want to mask off when you shift ie:

WORD myNumber = (((myNumber & 0x00ff) << 8) | ((myNumber & 0xff00) >> 8);

same with DWORD. It''s just safe practice



My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Advertisement
The correct method is to use the ntohl, ntohs, htonl and htons functions, since they are standard and made precisely for this purpose.
I can''t use the winsock fucntions, because the communication between this plc and a pc has to be done with inaclient. This is a protocol especialy made for this communcation. Only bad that they did not put the byteswap in the protocol.
They''re not technically winsock functions, they''re just standard networking functions, and should be suitable for any protocol, including one you make yourself.
How does that method work? It seems to me that it would reverse the 0th and 7th bits but clear the rest as zero. that doesnt seem to be right at all.

// God.c
void main() {
WORLD Earth;
LIFE People = Earth.CreateLife(HUMAN);
GiveHope(&People);
delete Earth;
EvilCackle();
}



(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)

This topic is closed to new replies.

Advertisement