#include "..\Include\sys.h"

//////////////////////////////////////////////////////////
char BCD_To_CHAR(char BCD)
{
    if ((BCD>=0)&&(BCD<=9))
    {
        return 0x30+BCD;
    }
    if (BCD==0x0A)
    {
        return 0x2A;
    }
    if (BCD==0x0B)
    {
        return 0x23;
    }
    if (BCD==0x0C)
    {
        return 'a'; //0x61;
    }
    if (BCD==0x0D)
    {
        return 'b'; //0x62;
    }
    if (BCD==0x0E)
    {
        return 'c'; //0x63;
    }
    if (BCD==0x0F)
    {
        return 0x00;
    }
    return 0x3F;
}

string  BCD2STR(OCTET& strBCD, bool bPositive, bool bEven)
{
    char Ch;
    char buf[50];
    int  m=0,BCDlen=strlen(strBCD,false)*2;

    if (BCDlen==0) return "";    
    
    if (!bEven) BCDlen=BCDlen-1;

    while(m<BCDlen)
    {
        strGetAt(strBCD, m>>1, Ch);
        if (bPositive)
            Ch=(m & 0x01)? (Ch):(Ch>>4);
          else
            Ch=(m & 0x01)? (Ch>>4):(Ch);
           
        buf[m]=BCD_TO_CHAR(Ch & 0x0F);
        
        m=m+1;
    }
    buf[m]=0;
    return strFormat("%s",buf);
}
               
//////////////////////////////////////////////////////////
struct tStringWithLenAndZeroEnd = String	
{
    UINT1  Len;
    String strData {    // string content
    
               void Translate(String strCurIdent,dynamic& curItem,long nIdxInArr)
               {    
                   sysFetchValue(strCurIdent,curItem,now(),this.len,true);
               } 
           };
    UINT1  Zero;
};
attribute name='tStringWithLenAndZeroEnd'
{
    String Integrate(String strCurIdent)   
    {
        if (this.Zero!=0)
           sysAbort(strCurIdent,"string not end with zero");
           
        return this.strData;
    }
};


