Re: swapping bytes in an integer
EventHelix.com wrote:[color=blue][color=green]
> > If I have a 32 bit unsigned int that is in the wrong byte order, how
> > can I convert it? For example, if have a number 0x090a0b0c how can I
> > reverse this to 0x0c0b0a09 ?[/color]
>
> The following article should help:
> http://www.eventhelix.com/RealtimeMa...ndOrdering.htm
>
> --
> EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
> Sequence Diagram Based System Design and Object Interaction Modeling
> Tool[/color]
[color=blue]
>From this web site...[/color]
Routines to convert between big-endian and little-endian formats are
actually quite straight forward. The routines shown below will convert
from both ways, i.e. big-endian to little-endian and back.
Big-endian to Little-endian conversion and back
short convert_short(s hort in)
{
short out;
char *p_in = (char *) ∈
char *p_out = (char *) &out;
p_out[0] = p_in[1];
p_out[1] = p_in[0];
return out;
}
long convert_long(lo ng in)
{
long out;
char *p_in = (char *) ∈
char *p_out = (char *) &out;
p_out[0] = p_in[3];
p_out[1] = p_in[2];
p_out[2] = p_in[1];
p_out[3] = p_in[0];
return out;
}
EventHelix.com wrote:[color=blue][color=green]
> > If I have a 32 bit unsigned int that is in the wrong byte order, how
> > can I convert it? For example, if have a number 0x090a0b0c how can I
> > reverse this to 0x0c0b0a09 ?[/color]
>
> The following article should help:
> http://www.eventhelix.com/RealtimeMa...ndOrdering.htm
>
> --
> EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
> Sequence Diagram Based System Design and Object Interaction Modeling
> Tool[/color]
[color=blue]
>From this web site...[/color]
Routines to convert between big-endian and little-endian formats are
actually quite straight forward. The routines shown below will convert
from both ways, i.e. big-endian to little-endian and back.
Big-endian to Little-endian conversion and back
short convert_short(s hort in)
{
short out;
char *p_in = (char *) ∈
char *p_out = (char *) &out;
p_out[0] = p_in[1];
p_out[1] = p_in[0];
return out;
}
long convert_long(lo ng in)
{
long out;
char *p_in = (char *) ∈
char *p_out = (char *) &out;
p_out[0] = p_in[3];
p_out[1] = p_in[2];
p_out[2] = p_in[1];
p_out[3] = p_in[0];
return out;
}
Comment