This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

64 Bits integer numbers

How can we manage a 64 bits integer
number to convert it to ASCCII value for LCD display.

Does SPRINTF work with 'double' data type

reguards.

  • I doubt that anyone would want to read a 20-digit number (which corresponds to 64 bits) from a display. What I would suggest is to convert such number to something shorter (say, 32 bits) and then to display it. This would be an approximation, but people do it all the time.
    Regards,
    Mike

  • The ISO 11784 norm for animal identification code Idf number on 38 bits so we need a data type greater than
    unsigned long.
    I ask for 64 bits because a 38 bits data type may not exist!

  • Sorry, I should have guessed it would be something like this.
    It's no easy task - to convert a 38-bit number into decimal. Octal and hexadecimal numbers suit much better because conversion routines for them are quite straightforward.
    I'm afraid in case of decimals one would have to re-invent integer arithmetics for 38-bit numbers.
    good luck
    mike

  • Does SPRINTF work with 'double' data type

    Please read the manual! (jst clik on SPRINTF in your code & press F1!)

    No, because C51 does not have a 'double' data type!

    But do you really need it in an arithmetic data type?
    ie, do you actually need to do any maths with it?
    If it's just an ID number, presumably not; so why not just store it as a string?

    As you're comtemplating sprintf, I assume you're not tight on space?
    But if you are, could you store it in a BCD format; 2 decimal digits to each byte?

  • How about this method. You will need to define an array of 38 elements. Each element is an unsigned long. Each element n is initialised to 2 to the power of n+1 and uses BCD representation.

    = { 0x00000001,
        0x00000002,
        0x00000004,
        0x00000008,
        0x00000016,
        0x00000032....etc. }
    
    Do do your conversion from binary to BCD, your application must add together the elements of the array for which there is a one in the original binary number. The application must use BCD arithmetic or course, that may be easier to do with an assembly language insert.

    If you want to avoid assembly language, I suppose you might use a two dimensional array i.e. and array of 38 elements where each element is itself an array of bytes. Each byte will take a value 0 to 9. A simple loop will be needed to perform the necessary additions.

  • Out of curiosity I came up with the following code to convert n binary digits to m decimal digits. This is the essence of a solution which may be readily adapted to your specific needs.

    The code includes a number of test examples.

    It compiles to reasonably efficient code, although making use of memory specific pointers would be much better than generic pointers.

    #define	BINARY_DIGITS	38
    #define	DECIMAL_DIGITS	12
    //															                    1                   2                   3
    //															0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 
    xdata	unsigned char big_binary_00[ BINARY_DIGITS ] 	= { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
    xdata	unsigned char big_binary_01[ BINARY_DIGITS ] 	= { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
    xdata	unsigned char big_binary_10[ BINARY_DIGITS ] 	= { 0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
    xdata	unsigned char big_binary_16[ BINARY_DIGITS ] 	= { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
    xdata	unsigned char big_binary_38[ BINARY_DIGITS ] 	= { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 };
    xdata	unsigned char big_binary_xx[ BINARY_DIGITS ] 	= { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };
    
    xdata	unsigned char big_decimal[ DECIMAL_DIGITS ];
    
    void conversion( unsigned char *binary, unsigned char *decimal )
    {
    	unsigned char	loop;
    	unsigned char	outer_loop;
    	unsigned char	*d;
    	unsigned char	*t;
    	unsigned char	carry = 0;
    	unsigned char	temp[ DECIMAL_DIGITS ];
    
    	//
    	//	Initialise Data
    	//
    
    	loop = DECIMAL_DIGITS;
    	d = decimal;
    	t = &temp[ 0 ];
    
    	do
    	{
    
    		*t = 0;
    		t++;
    		*d = 0;
    		d++;
    		
    	}while( --loop != 0 );	
    
    	temp[ 0 ] = 1;
    
    
    	outer_loop = BINARY_DIGITS;
    
    	do
    	{
    		if( *binary != 0 )
    		{
    	
    			//
    			//	Set d to d plus t
    			//
    		
    			loop = DECIMAL_DIGITS;
    			carry = 0;
    			t = &temp[ 0 ];
    			d = &decimal[ 0 ];
    		
    			do
    			{
    				*d = (*d + *t) + carry;
    		
    				if( *d < 10 )
    				{
    					carry = 0;
    				}
    				else
    				{
    					*d = *d - 10;
    					carry = 1;
    				}
    		
    				d++;
    				t++;
    		
    			}while( --loop != 0 );	
    			
    		
    		}
    	
    		//
    		//	Double the BCD value in t
    		//
    		
    		loop = DECIMAL_DIGITS;
    		carry = 0;
    		t = &temp[ 0 ];
    		
    		do
    		{
    			*t = (*t * 2) + carry;
    		
    			if( *t < 10 )
    			{
    				carry = 0;
    			}
    			else
    			{
    				*t = *t - 10;
    				carry = 1;
    			}
    		
    			t++;		
    		
    		}while( --loop != 0 );
    
    		binary++;
    
    	}while( --outer_loop != 0 );	
    
    }
    
    
    
    
    main( void )
    {
    	conversion( &big_binary_00, &big_decimal );
    	conversion( &big_binary_01, &big_decimal );
    	conversion( &big_binary_10, &big_decimal );
    	conversion( &big_binary_16, &big_decimal );
    	conversion( &big_binary_38, &big_decimal );
    
    	conversion( &big_binary_xx, &big_decimal );
    	conversion( &big_binary_xx, &big_decimal );
    }