#include		<stdio.h>

#define			unchar			unsigned char

FILE			*fpw0;

	char			pure_dat[100];
	char			xcent;
	char			ycent;
	char			xpos[100];
	char			ypos[100];
	char			chnum_h[100];
	char			chnum_l[100];

	char			read_dat0[100];

	int			data_sum,i;

main(int argc, char **argv)
{
	/***** read file open *****/
	fpw0 = fopen("pos.dat","r");
	fgets(&pure_dat, 10, fpw0);
	strcpy(read_dat0, pure_dat);
	read_dat0[2] = '\0';
	ycent = (char) strtol(read_dat0, (char **)NULL, 16);
	strcpy(read_dat0, pure_dat+3);
	read_dat0[2] = '\0';
	xcent = (char) strtol(read_dat0, (char **)NULL, 16);

	data_sum = 0;
	while((fgets(&pure_dat, 100, fpw0))!=NULL){
		strcpy(read_dat0, pure_dat);
		read_dat0[2] = '\0';
		ypos[data_sum] = (char) strtol(read_dat0, (char **)NULL, 16);

		strcpy(read_dat0, pure_dat+3);
		read_dat0[2] = '\0';
		xpos[data_sum] = (char) strtol(read_dat0, (char **)NULL, 16);

		strcpy(read_dat0, pure_dat+6);
		read_dat0[1] = '\0';
		chnum_h[data_sum] = (char) strtol(read_dat0, (char **)NULL, 16);

		strcpy(read_dat0, pure_dat+7);
		read_dat0[2] = '\0';
		chnum_l[data_sum] = (char) strtol(read_dat0, (char **)NULL, 16);

		data_sum++;
	}

	/***** read file close *****/
	fclose(fpw0);

	/***** write file open *****/
	fpw0 = fopen("cpos.dat","w");

	for(i=0;i<data_sum;i++){
		fprintf(fpw0, "\t\tBYTE\t0%02XH,0%02XH\n",(unchar)(0Xff & (xpos[i]-xcent)) ,(unchar)(0XFF & (ypos[i]-ycent)));
		fprintf(fpw0, "\t\tHEX\t%02X,%02X,02,00\n",(unchar)chnum_l[i], (unchar)chnum_h[i]);
	}

	/***** write file close *****/
	fclose(fpw0);

	exit(0);
}
