#include "includes.h"


//****************************************************************************
// bin2dec()
// konverze integer cisla do dekadickeho formatu
// vysledek ulozen v poli *buff, fill je pocet cislic zprava
//****************************************************************************
void bin2dec( tUChar *buff, tUInt to_convert, tUChar fill )   
{   
     tUInt tenthous;   
     tUInt thousands; 
     tUInt hundreds;   
     tUInt tens;   
     tUInt ones;   

//reset values   
     tenthous = 0;   
     thousands = 0;                 
     hundreds = 0;   
     tens = 0;   
     ones = 0;   
     
	 while (to_convert > 0x0)   
     {   
           
        if (to_convert > 0x270F)   
        {   
            tenthous++;   
            to_convert -= 0x2710;   
        }   
        else if(to_convert > 0x03E7 && to_convert <= 0x270F)           
        {   
            if (thousands < 9)   
            {   
                thousands++;               
            }   
            else   
            {   
                tenthous++;   
                thousands=0;   
            }   
            to_convert -= 0x3E8;            
        }   
        else if(to_convert > 0x63 && to_convert <= 0x3E7)          
        {   
            if (hundreds < 9)   
            {   
                hundreds++;            
            }   
            else    
            {   
                hundreds = 0;              
                if (thousands < 9)   
                {   
                    thousands++;               
                }   
                else   
                {   
                    tenthous++;   
                    thousands=0;   
                }   
            }   
            to_convert -=0x64;   
        }   
        else if(to_convert > 0x9 && to_convert <= 0x63)        
        {   
            if (tens < 9)   
            {   
                tens++;        
            }   
            else    
            {   
                   
                tens = 0;      
                if (hundreds < 9)   
                {   
                    hundreds++;            
                }   
                else    
                {   
                    hundreds = 0;              
                    if (thousands < 9)   
                    {   
                        thousands++;               
                    }   
                    else   
                    {   
                        tenthous++;   
                        thousands=0;   
                    }      
                }   
            }   
            to_convert -= 0xA;   
        }   
        else if(to_convert >= 0x1 && to_convert <= 0x9)        
        {   
            if (ones < 9)   
            {   
                ones++;        
            }   
            else   
            {   
                ones = 0;   
                if (tens < 9)   
                {   
                    tens++;        
                }   
                else    
                {   
                    tens = 0;   
                    if (hundreds < 9)   
                    {   
                        hundreds++;   
                    }   
                    else    
                    {   
                        hundreds = 0;   
                        if (thousands < 9)   
                        {   
                            thousands++;   
                        }   
                        else   
                        {   
                            tenthous++;   
                            thousands=0;   
                        }   
                    }   
                }   
            }   
            to_convert -= 1;   
        }   
           
     }   
 /*   tenthous += 0x30;       //Store all conversions in ASCII form   
    thousands += 0x30;   
    hundreds += 0x30;   
    tens += 0x30;   
    ones += 0x30;   */

	 if(fill == 1)
	  {
	   buff[0] = ones;
	  }

	 if(fill == 2)
	  {
	   buff[0] = tens;
	   buff[1] = ones;
	  }
	   
	 if(fill == 3)
	  {
	   buff[0] = hundreds;
	   buff[1] = tens;
	   buff[2] = ones;
	  }

	 if(fill == 4)
	  {
	   buff[0] = thousands;
	   buff[1] = hundreds;
	   buff[2] = tens;
	   buff[3] = ones;
	  }

	   
}    
