diff -r 000000000000 -r 0c6405ab2ff4 npp/02.read_uint16_ASCII.c.x --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/npp/02.read_uint16_ASCII.c.x Mon Jan 26 22:08:20 2009 -0500 @@ -0,0 +1,90 @@ +/* purpose: conversion of binary data with little-endian to ASCII + Sep. 8, 2006 by Maosheng Zhao */ + +/* I also accounts for the possible different CPU on different platforms, + and therefore, it should work on different machines without problem */ + +#include +#define COLS 7200 +#define ROWS 3600 +#define DATA_TYPE unsigned short + +/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/ +#define IN_FILE_NAME "Npp_0.05deg_mean.int16" +#define OUT_FILE_NAME "Npp_0.05deg_mean.ASCII2" +/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/ +int main() +{ + /* definiation of variables will be used */ + FILE *in_f, *out_f; + static DATA_TYPE data_in[ROWS][COLS]; + int i,j; + + /* the follow definition is for determination the type of CPU */ + int is_big_endian=0; + short one=1; + char *cp; + + /* define a buffer pointer to swap bytes if it is big-endian CPU */ + char temp_char[sizeof(DATA_TYPE)]; + DATA_TYPE *buffer_data; + int m; + + /*################################################################*/ + /* main program starts */ + /* open the data file and read all the data */ + in_f = fopen(IN_FILE_NAME,"rb"); + if(in_f == NULL) + { + printf("cannot open %s, exit...\n",IN_FILE_NAME); + exit(0); + } + else /* reading the data */ + { + /* tell the type of CPU */ + cp = (char*)&one; + if(*cp == 0) + is_big_endian = 1; /* yes, this is a big-endian CPU */ + else + is_big_endian = 0; /* this is a little-endian CPU */ + + if(is_big_endian == 0) + { + printf("This is a little-endian CPU, no need to swap bytes\n"); + printf("reading data now, please waiting for...\n"); + fread(data_in,sizeof(DATA_TYPE),COLS*ROWS,in_f); + } + else + { + printf("This is a big-endian CPU, which needs to swap bytes\n"); + printf("reading data now, please waiting for...\n"); + for(i=0;i