[R-gregmisc-users] SF.net SVN: r-gregmisc: [1145] trunk/SASxport/src/reverse.c
Brought to you by:
warnes
From: <wa...@us...> - 2007-08-15 06:06:11
|
Revision: 1145 http://r-gregmisc.svn.sourceforge.net/r-gregmisc/?rev=1145&view=rev Author: warnes Date: 2007-08-14 23:06:09 -0700 (Tue, 14 Aug 2007) Log Message: ----------- Fix reverse.c because we need to swap everything to match Big-Endian, rather than Little-Endian. Also, dont' call the macro. Modified Paths: -------------- trunk/SASxport/src/reverse.c Modified: trunk/SASxport/src/reverse.c =================================================================== --- trunk/SASxport/src/reverse.c 2007-08-15 06:04:57 UTC (rev 1144) +++ trunk/SASxport/src/reverse.c 2007-08-15 06:06:09 UTC (rev 1145) @@ -1,4 +1,4 @@ -#include "writeSAS.h" +//#include "writeSAS.h" #include <stdio.h> #include <string.h> @@ -6,30 +6,29 @@ #import <assert.h> #import <sys/types.h> -/* reverse: convert current byte order to little endian */ -void reverse( u_char *intp, size_t size) +/* reverse: convert current byte order to big endian */ +void reverse( unsigned char *intp, size_t size) { - static u_char endianTest[2] = {0x01,0x00}; + static unsigned char endianTest[2] = {0x01,0x00}; size_t i; - u_char tmp; + unsigned char tmp; #if !defined(BIG_ENDIAN) && !defined(LITTLE_ENDIAN) /* Test if we are on a big endian or little endian platform */ - if( (short) *endianTest == 1 ) + if( (short) *endianTest != 1 ) { - /* Little Endian */ - /* Do nothing */ + /* The native byte order is big endian, so do nothing */ + sprintf("Big Endian Machine!\n"); return; } #endif - /* Big Endian */ - /* Swap bytes */ + /* If native byte order is little endian, we need to swap bytes */ for(i=0; i < size/2; i++) { - tmp = (u_char) intp[i]; + tmp = (unsigned char) intp[i]; intp[i] = intp[size-i-1]; - intp[size-i] = tmp; + intp[size-i-1] = tmp; } return; @@ -39,41 +38,41 @@ /* test code */ void test_reverse() { - u_char byte_pattern[1] = { 0x00 }; - u_char byte_value = 0x00; + unsigned char byte_pattern[1] = { 0x00 }; + unsigned char byte_value = 0x00; - u_char short_pattern[2] = { 0x00, 0x01 }; /* NB: little endian byte pattern */ - short short_value = 0x0100; /* NB: hex is written big endian */ + unsigned char short_pattern[2] = { 0x01, 0x00 }; /* NB: big endian byte pattern */ + short short_value = 0x0100; /* NB: hex is also written big endian */ - u_char int_pattern[4] = { 0x0, 0x01, 0x02, 0x03 }; + unsigned char int_pattern[4] = { 0x03, 0x02, 0x01, 0x00 }; int int_value = 0x03020100; - u_char long_pattern[4] = { 0x0, 0x01, 0x02, 0x03 }; + unsigned char long_pattern[4] = { 0x03, 0x02, 0x01, 0x00 }; long long_value = 0x03020100; /* Do the reverse, then test */ /* byte */ - REVERSE( &byte_value, sizeof(u_char) ); - assert( (u_char) *byte_pattern == byte_value ); + reverse( &byte_value, sizeof(unsigned char) ); + assert( (unsigned char) *byte_pattern == byte_value ); /* short */ - REVERSE( &short_value, sizeof(short) ); + reverse( (unsigned char*) &short_value, sizeof(short) ); assert( *((short *) short_pattern) == short_value ); /* int */ - REVERSE( &int_value, sizeof(int) ); + reverse( (unsigned char*) &int_value, sizeof(int) ); assert( *((int *) int_pattern) == int_value ); /* long */ - REVERSE( &long_value, sizeof(long) ); + reverse( (unsigned char*) &long_value, sizeof(long) ); assert( *((long*) long_pattern) == long_value ); } #ifdef DO_TEST -int main(int argc, u_char *argv) +int main(int argc, char *argv) { test_reverse(); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |