ARM Technical Support Knowledge Articles

CONVERTING BIG ENDIAN TO LITTLE ENDIAN

Applies to: C51 C Compiler

Answer


Information in this article applies to:


QUESTION

I have a memory mapped 16 bit peripheral that is Little Endian in format. How do I swap the order of the bytes in the word that I write out to the peripheral?

ANSWER

There are numerous ways to swap bytes in a 16-bit value. The following example uses the Mem_Swap function to swap the order of the bytes in the word argument that is passed. The new value for the word is returned.

// Memory Mapped I/O Device
xdata unsigned int io_dev _at_ 0x8000;

// Byte-swapping Function
unsigned int Mem_Swap (unsigned int value)
  {
  unsigned int rval;

  ((unsigned char *) &rval) [0] = ((unsigned char *) &value) [1];
  ((unsigned char *) &rval) [1] = ((unsigned char *) &value) [0];

  return (rval);
  }

void main (void)
  {
  io_dev = Mem_Swap (0xaa55);
  while (1){};
  }

Article last edited on: 2000-01-18 00:00:00

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential