byte GBF_ConvertBCDToByteArr(byte bcdRawData[], word numOfBCDs, byte outBCDArr[]){
word i;
word byteIndex;long tmpVal;
byte retVal;char tmpStr[10];char tmpErrStr[gcText200];
retVal = gcNok;// VS/dsa: Reset output arrayfor(i =0; i <elcount(outBCDArr); i++){
outBCDArr[i]=0;}//VS/dsa: Check that the supplied data can be used to create the specified number of BCDsif((elcount(bcdRawData)*2)< numOfBCDs){snprintf(tmpErrStr, elcount (tmpErrStr),"GBF_ConvertBCDToByteArr: ERROR: Input parameters do not match, numOfBCDs is %d but bcdRawData only contains %d elements!", numOfBCDs,elcount(bcdRawData));write(tmpErrStr);}else{// VS/dsa: Also check that the supplied output array can hold all BCD values if(elcount(outBCDArr)<(numOfBCDs)){
write ("GBF_ConvertBCDToByteArr: ERROR: Input parameters do not match, numOfBCDs is %d but outBCDArr only contains %d elements!", numOfBCDs,elcount(outBCDArr));}else{//VS/dsa: All checks went fine, convert datafor(i =0; i < numOfBCDs; i++){
byteIndex = i /2;if(0==(i %2)){
outBCDArr[i]=(byte)(bcdRawData[byteIndex]>>4);}else{
outBCDArr[i]=(byte)(bcdRawData[byteIndex]&0x0F);}}
retVal = gcOk;}}return retVal;}
byte GBF_ConvertBCDToStr(byte bcdRawData[], word numOfBCDs,char outBCDStr[]){
word i;
word byteIndex;long tmpVal;
byte retVal;char tmpStr[10];char tmpErrStr[gcText200];// VS/dsa: Init to failed
retVal = gcNok;// VS/dsa: Reset output stringstrncpy(outBCDStr,"",elcount(outBCDStr));//VS/dsa: Check that the supplied data can be used to create the specified number of BCDsif((elcount(bcdRawData)*2)< numOfBCDs){snprintf(tmpErrStr, elcount (tmpErrStr),"GBF_ConvertBCDToStr: ERROR: Input parameters do not match, numOfBCDs is %d but bcdRawData only contains %d elements!", numOfBCDs,elcount(bcdRawData));write(tmpErrStr);}else{// VS/dsa: Also check that the supplied output array can hold all BCD values and a string terminator character if(elcount(outBCDStr)<(numOfBCDs +1)){snprintf(tmpErrStr, elcount (tmpErrStr),"GBF_ConvertBCDToStr: ERROR: Input parameters do not match, numOfBCDs is %d but outBCDStr only contains %d elements!", numOfBCDs,elcount(outBCDStr));write(tmpErrStr);}else{//VS/dsa: All checks went fine, convert datafor(i =0; i < numOfBCDs; i++){// VS/dsa: The byte index to use for accessing bcdRawData is e.g. 0 if i == 0 or 1 and 1 if i == 3 or 4
byteIndex = i /2;// VS/dsa: Use the modulo operator % to check if this is an even or odd iteration// For even iterations just shift the most significant four bits four positions to the right// For odd iterations filter out the least significant four bits, no shift neededif(0==(i %2)){
tmpVal =(long)(bcdRawData[byteIndex]>>4);}else{
tmpVal =(long)(bcdRawData[byteIndex]&0x0F);}// VS/dsa: Convert value to a temp string. Always use base 10 for conversion since this is BCD.ltoa(tmpVal, tmpStr,10);// VS/dsa: Build output string. Note: Since each value is appended at the end of the string, we must start the iteration// with the most significant valuestrncat(outBCDStr, tmpStr,elcount(outBCDStr));}
retVal = gcOk;}}return retVal;}