#include int LoadAkkuvk(unsigned char *,unsigned char,unsigned char *); int LoadAkkunk(unsigned char *,unsigned char,unsigned char *); int ShiftAkku(unsigned char *, int,int); unsigned char DivAkku(unsigned char *, unsigned char divisor, unsigned char *); unsigned char MulAkku(unsigned char *, unsigned char, unsigned char *); int sprintff(char *, char *, ...); int my_itoa(unsigned char *, unsigned char, unsigned char *, char *); int getNumber (char **pformat) { int value; value=0; while(**pformat >= '0' && **pformat <= '9') { value *= 10; value=value+ (**pformat & 0x0f); (*pformat)++; } (*pformat)--; return value; } int sprintff(char buff[], char *format, ...) { #define LONG 1 #define SHORT 2 unsigned char akkuvk[17],akkunk[17],akkuerg[17],divisor; unsigned char mant[3],*m,*pmant,exxp,vorz,rest, endetest; int bit,start_pos,end_pos; double dvalue, *dpoint; float fvalue, *fpoint; char *va_point,*anf_index,*ppointer,Form,**ppchar,*pchar; int length, frac, wasPoint, modifier,anzahl,textlaenge,j,Diff,dexxp; long value,*plong; int buff_index,i,*pint; short *pshort; char my_buffer[100]; buff_index=0; va_point=(char *)&format; va_point += sizeof(format); while(*format != '\0') { if(*format == '%') { anf_index=format; format++; if(*format == '%') { anf_index=format; buff[buff_index]=*format; buff_index++; } else { //could be a format-statement length = -1; frac = -1; wasPoint = 0; modifier = 0; vorz=0; while((*format != 'd') &&(*format != 'i') &&(*format != 'o') &&(*format != 'x') &&(*format != 'X') &&(*format != 'u') &&(*format != 'c') &&(*format != 's') &&(*format != 'f') &&(*format != 'e') &&(*format != 'E') &&(*format != 'g') &&(*format != 'G') &&(*format != '\0')) { if (*format == '.') { wasPoint = 1; } else { if (*format >= '0' && *format <= '9') { value = getNumber(&format); if (wasPoint != 0) { frac = value; //behind the point } else { length = value; //before the point } } else { if (*format == 'l') { modifier = LONG; } else { if (*format == 'h') { modifier = SHORT; } else { //error } } } } format++; } ; if(*format == 'd' || *format == 'i' || *format == 'u' || *format == 'X' || *format == 'x' || *format == 'o' || *format == 'c' || *format == 's') { if(*format == 's') { ppchar=(char **)va_point; pchar=*ppchar; va_point += sizeof(char *); } else { if (modifier == SHORT) { pshort=(short*)va_point; value = (int)*pshort; va_point += sizeof(short); } else { if (modifier == LONG) { plong=(long*)va_point; value = (int) *plong; va_point += sizeof(long); } else { pint=(int*)va_point; value = *pint; va_point += sizeof(int); } } } if(*format == 'c') { my_buffer[0]=(char)value; anzahl=1; } else { if(*format == 's') { start_pos=buff_index; if(frac == -1) { while(*pchar != '\0') { buff[buff_index++]=*pchar; pchar++; } } else { anzahl=0; while((*pchar != '\0') && (anzahl < frac)) { buff[buff_index++]=*pchar; pchar++; anzahl++; } } } else { // if signed and the value is negative if((value < 0) && ((*format == 'd') || (*format == 'i'))) { value=-value; vorz=1; } ppointer=(char *)&value; for(i=0;i<=17;i++) { akkuvk[i]=0; } for(i=0;i 0x39) { if(*format == 'X') { my_buffer[i] += 0x07; //offset upper case } else { my_buffer[i] += 0x27; //offset lower case } } } } } } if(*format != 's') { if(vorz == 1) { buff[buff_index]='-'; buff_index++; } start_pos=buff_index; if(frac > anzahl) { i=frac; } else { i=anzahl; } j=0; buff_index=buff_index+i-1; for( ;i>0;i--) { if(j< anzahl) { buff[buff_index-j]=my_buffer[j++]; } else { buff[buff_index-j]='0'; j++; } } buff_index++; } } else { if((*format == 'f') || (*format == 'e') || (*format == 'E') || (*format == 'g') || (*format == 'G')) { if((*format == 'e') || (*format == 'E')) { Form='e'; } if(*format == 'f') { Form='f'; } if(frac < 0) // number of digits behind the decimal point { frac = 6; } /* the following comments are to be changed in case of double precision compilers */ /* dpoint=(double *)va_point; dvalue=*dpoint; va_point += sizeof(double); */ fpoint = (float *)va_point; fvalue= *fpoint; va_point += sizeof(float); //fvalue=dvalue; // mant[0]=0; mant[1]=0; mant[2]=0; pmant=mant; m=(unsigned char*)&fvalue; *pmant=*m; //lower bytes of mantissa copied pmant++; m++; *pmant=*m; m++; pmant++; *pmant=(*m)| 0x80; //highest bit of mantissa reconstructed bit = (*m) & 0x80; //bit == 1, if the lowest exponent-bit ==1 //and ==0, if the lowest exponent-bit ==0 m++; exxp=(*m) << 1; //extract the sign-bit of the mantisse from the Byte if(bit != 0) //and get the correct exponent value { exxp+=1; } if(((*m) & 0x80) > 0) { vorz=1; //vorz=0x80(--> neg) or 0x00 (--> pos) } else { vorz=0; } // now mant has the complete mantissa right justified // and in exxp is the correct exponent // vorz is -1, if the value is negative and ==0, if the value is positive // transformation from IEEE-format to mantissa and exponent(bias 127) is ready // if lower then 105, the precision of the mantissa decrease // then switch to exponent-representation // the same if value is higher then 149 LoadAkkuvk(mant,exxp,akkuvk); LoadAkkunk(mant,exxp,akkunk); for(i=0;i<=99;i++) { my_buffer[i]=' '; } //akkuvk[] is loaded via LoadAkkuvk in the correct way // my_itoa writes to my_buffer beginning at index 0, reverse order anzahl=my_itoa(akkuvk,10,akkuerg,my_buffer); if((*format == 'g') || (*format == 'G')) { if(anzahl > 0) { if(frac >= anzahl) { Form='f'; frac=frac-anzahl; } else { Form='e'; frac=frac-1; } } else { //perhaps realize as a function j=51; endetest=0; do { rest=MulAkku(akkunk,10,akkuerg); my_buffer[j]=rest | 0x30; for(i=0;i<=15;i++) { akkunk[i]=akkuerg[i]; if(akkunk[i] != 0) { endetest=1; } } j++; } while((endetest != 0) && (j<100)); dexxp=-1; for(i=0;((i<49) && (my_buffer[i+51] == 0x30));i++) { dexxp--; } if(dexxp <= -39) { Form='f'; } else { if(dexxp <= -4) { Form='e'; frac=frac-1; } else { Form='f'; frac=frac - dexxp-1; } } } } if(vorz != 0) { buff[buff_index] = '-'; buff_index++; } start_pos=buff_index; // store first position for rounding if(Form =='f') { for(i=0;i (frac + 1 + 2)) { Diff=0; for(i=0;i 0) { Diff=frac+1+2-anzahl; for(i=0;i=38) { dexxp=0; for(j=0;j0) { for(i=0;i 0x34) || ((my_buffer[i+51] == 0x34) && (my_buffer[i+52] > 0x34))) { while((end_pos >= start_pos) && ((buff[end_pos] == 0x39) || (buff[end_pos] == '.'))) { if(buff[end_pos] != '.') { buff[end_pos] = 0x30; } end_pos--; } if(end_pos < start_pos) { if(Form == 'f') { for(i=buff_index;i>start_pos;i--) { buff[i]=buff[i-1]; } buff[start_pos]=0x31; buff_index++; } if(Form == 'e') { buff[start_pos]=0x31; dexxp=dexxp+1; } } else { buff[end_pos]++; } } //und hier endet es if(Form == 'e') { if((*format == 'e') || (*format == 'g')) { buff[buff_index++]='e'; } else { buff[buff_index++]='E'; } if(dexxp<0) { buff[buff_index++]='-'; dexxp=-dexxp; } else { buff[buff_index++]='+'; } i=3; do { i--; rest=dexxp%10; dexxp=dexxp/10; buff[buff_index+i]=rest | 0x30; } while(dexxp != 0); while(i>0) { i--; buff[buff_index+i]=0x30; } buff_index=buff_index+3; } } else { // format not ok -> print it anf_index++; while (anf_index <= format) { buff[buff_index] = *anf_index; buff_index++; anf_index++; } format--; } } //start of adjusting the wide of the field textlaenge=buff_index-start_pos+vorz; if(textlaenge < length) { for(i=buff_index;i> (start_pos - vorz);i--) { buff[i+length-textlaenge-1]=buff[i-1]; } buff_index=buff_index+length-textlaenge; if(vorz > 0) { buff[i]='-'; } for(i=start_pos-vorz;i255) { carryin=1; akkuerg[j]=zwiwert-256; } else { carryin=0; akkuerg[j]=zwiwert; } } } ShiftAkku(zwakku,1,16); } return akkuerg[16]; } unsigned char DivAkku(unsigned char akku1[], unsigned char divisor, unsigned char akkuerg[]) { int i; unsigned char dividend,nByte,nBit,rest,maske; for(i=0;i<=15;i++) { akkuerg[i]=0; } dividend=0; for(i=127;i>=0;i--) { dividend=dividend << 1; nByte=i/8; //gets the Byte-Index for the akku nBit=i%8; //gets the Bit_index in the Bytes //get new Bit from top maske=0x01; maske=maske << nBit; if((akku1[nByte] & maske) != 0) { dividend +=1; } if(dividend >= divisor) { akkuerg[nByte]=akkuerg[nByte] | maske; dividend=dividend-divisor; } } rest=dividend; return rest; } int LoadAkkunk(unsigned char mantisse[], unsigned char exxpo,unsigned char akkunk[]) { int i, nShift; for(i=0;i<=15;i++) { akkunk[i]=0; } akkunk[13]=mantisse[0]; akkunk[14]=mantisse[1]; akkunk[15]=mantisse[2]; nShift=(exxpo-127)+1; ShiftAkku(akkunk,nShift,15); return 0; } int LoadAkkuvk(unsigned char mantisse[],unsigned char exxpo,unsigned char akkuvk[]) { //Loads the mantissa into the akku1 //shifts the mantissa int i,nShift; for(i=0;i<=15;i++) { akkuvk[i]=0; } akkuvk[0]=mantisse[0]; akkuvk[1]=mantisse[1]; akkuvk[2]=mantisse[2]; nShift=(exxpo-127)-23; ShiftAkku(akkuvk,nShift,15); //only digits left from the decimal point in akkuvk return 0; } int ShiftAkku(unsigned char akku1[],int nShift,int high_index) //Shifts akku1 by nShift. If nShift >0 then to the left else to the right //akkuindex=15 is left (high-Bytes) und akkuindex=0 is right (low-bytes) //high_index is the highest Index of the array, if arreay higher then 0...15 { int i,j; unsigned char add; if(nShift > 0) { for(i=1;i<=nShift;i++) { for(j=high_index;j>=0;j--) { akku1[j]=akku1[j]<<1; if(j!= 0) { add=akku1[j-1] & 0x80; add=add>>7; } else { add=0; } akku1[j]+=add; } } } if(nShift < 0) { for(i=nShift;i!= 0;i++) { for(j=0;j<=high_index;j++) { akku1[j]=akku1[j]>>1; if(j!=high_index) { add=akku1[j+1] & 0x01; add=add << 7; } else { add=0; } akku1[j]+=add; } } } return 0; }