Click here to Skip to main content
16,007,885 members
Home / Discussions / C#
   

C#

 
GeneralRe: Endian Pin
lost in transition 31-May-07 9:51
lost in transition 31-May-07 9:51 
GeneralRe: Endian Pin
Luc Pattyn31-May-07 10:03
sitebuilderLuc Pattyn31-May-07 10:03 
GeneralRe: Endian Pin
lost in transition 31-May-07 10:21
lost in transition 31-May-07 10:21 
GeneralRe: Endian Pin
Luc Pattyn31-May-07 10:51
sitebuilderLuc Pattyn31-May-07 10:51 
GeneralRe: Endian Pin
lost in transition 31-May-07 11:47
lost in transition 31-May-07 11:47 
GeneralRe: Endian Pin
Luc Pattyn31-May-07 12:00
sitebuilderLuc Pattyn31-May-07 12:00 
GeneralRe: Endian Pin
lost in transition 1-Jun-07 4:58
lost in transition 1-Jun-07 4:58 
GeneralRe: Endian Pin
Luc Pattyn1-Jun-07 6:14
sitebuilderLuc Pattyn1-Jun-07 6:14 
Hi,

the example I gave was handling unsigned shorts, these hold two bytes, hence the
formula included just one OR operator.
For (unsigned) ints, you need to swap 4 bytes, hence 3 OR operators; plus some
masking to avoid bit aliasing.

You could also do it pseudo-recursively like so:

// Swaps all bytes of a uint
public static uint Swap(uint val) {
return (uint)( swapLowerTwoBytes(val)<<16 | swapLowerTwoBytes(val>>16) );
}

// Swaps a ushort (but accepts and returns it as a uint)
public static uint swapLowerTwoBytes(uint val) {
ushort sval=(ushort)val; // throw away top bytes
return (ushort) ( (sval<8) | (sval>8) );
}

Both methods look very similar, I added the (uint) cast for clarity although
it is not needed; both (ushort) casts are necessary to throw away irrelevant bytes.

For signed int, it is similar, but be careful the sign bit gets replicated when
shifting right; either use an explicit AND, or cast to unsigned first.

Hope this helps.

BTW: when I told you to use a recognizable hex test value, the basic idea
is to do all printout in hex too !

Smile | :)



GeneralRe: Endian Pin
lost in transition 1-Jun-07 8:52
lost in transition 1-Jun-07 8:52 
GeneralRe: Endian Pin
Luc Pattyn1-Jun-07 10:08
sitebuilderLuc Pattyn1-Jun-07 10:08 
GeneralRe: Endian Pin
lost in transition 31-May-07 11:52
lost in transition 31-May-07 11:52 
GeneralRe: Endian Pin
Luc Pattyn31-May-07 12:05
sitebuilderLuc Pattyn31-May-07 12:05 
AnswerRe: Endian Pin
lost in transition 31-May-07 9:43
lost in transition 31-May-07 9:43 
QuestionPlease Help: ClickOnce Technology Pin
salman_syed_0131-May-07 4:14
salman_syed_0131-May-07 4:14 
AnswerPlease do not double post Pin
leckey31-May-07 4:38
leckey31-May-07 4:38 
AnswerRe: Please Help: ClickOnce Technology Pin
mikker_1232-Jun-07 17:08
mikker_1232-Jun-07 17:08 
QuestionLog4 Net SMS Appender Pin
Cape Town Developer31-May-07 4:04
Cape Town Developer31-May-07 4:04 
AnswerRe: Log4 Net SMS Appender Pin
Cape Town Developer31-May-07 20:48
Cape Town Developer31-May-07 20:48 
AnswerRe: Log4 Net SMS Appender Pin
Cape Town Developer31-May-07 20:54
Cape Town Developer31-May-07 20:54 
QuestionPlease Help Me: ClickOnce Technology Pin
salman_syed_0131-May-07 4:02
salman_syed_0131-May-07 4:02 
Questionremove the datagridview "pointer" Pin
FernandoMartin31-May-07 3:54
FernandoMartin31-May-07 3:54 
AnswerRe: remove the datagridview "pointer" Pin
Not Active31-May-07 5:22
mentorNot Active31-May-07 5:22 
GeneralRe: remove the datagridview "pointer" Pin
FernandoMartin31-May-07 5:31
FernandoMartin31-May-07 5:31 
AnswerRe: remove the datagridview "pointer" Pin
Tarakeshwar Reddy31-May-07 6:44
professionalTarakeshwar Reddy31-May-07 6:44 
GeneralRe: remove the datagridview "pointer" Pin
FernandoMartin31-May-07 7:37
FernandoMartin31-May-07 7:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.