4 Replies - 10022 Views - Last Post: 12 July 2016 - 02:06 PM Rate Topic: -----

#1 HELPME!   User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 70
  • Joined: 06-April 09

Endian Swap

Posted 21 October 2009 - 02:50 AM

Description: Swap integer from big endian to little endian
int end_swp(int *num)
{
	int tmp, i;
	char *in, *out;
	
	in = (char *)num;
	out = (char *)&tmp;
	for (i = 3; i >= 0; i--)
	{
		out[i] = (*in++);
	}
	return tmp;
}

Is This A Good Question/Topic? 0
  • +

Replies To: Endian Swap

#2 aloneprogrammer   User is offline

  • New D.I.C Head

Reputation: -13
  • View blog
  • Posts: 31
  • Joined: 22-December 10

Re: Endian Swap

Posted 12 September 2012 - 08:19 PM

thanks for this useful for me(OS dev)
Was This Post Helpful? 0
  • +
  • -

#3 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Endian Swap

Posted 12 July 2016 - 11:06 AM

This assumes a 4-byte integer, which is not guaranteed. You should change your starting loop iterator from 3 to sizeof(int)
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14776
  • View blog
  • Posts: 59,069
  • Joined: 12-June 08

Re: Endian Swap

Posted 12 July 2016 - 11:20 AM

jjl - this is seven years old (from 2009).. is it a bad snippet?
Was This Post Helpful? 0
  • +
  • -

#5 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Endian Swap

Posted 12 July 2016 - 02:06 PM

It was one of the most recent post on the C snippets page, it should have sizeof(int) to be compatible with system who's sizeof(int) does not equal 4 bytes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1