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

Converting a binary coded decimal value to hexadecimal

Hello,

I'm new to the 8051 and I am hoping someone has had to solve a similar problem to this before, although this is not an 8051 specific problem.

Can someone give me assistance on how to convert a 12-digit binary coded decimal value to hexadecimal on the 8051? The BCD value is packed into 6 consecutive bytes.

Any help would be appreciated.

Regards,

Stephen McSpadden.

Parents
  • The following code will convert the array of BCD digits in variable bcd to a 32-bit binary value in hex.

    Of course, a 32-bit binary cannot hold the full range of values that you can get from 12 decimal digits. This code just indicates how to go about the conversion.

    main( void )
    {
    
        data unsigned char bcd[6] = { 0x00, 0x00, 0x12, 0x34, 0x56, 0x78 };
        data unsigned char loop;
        data unsigned char data *p;
        data unsigned long hex;
    
        hex = 0;
    
        loop = sizeof( bcd );
    
        p = &bcd[0];
    
        do
        {
            hex = hex * 10;
    
            hex = hex + ( *p >> 4 );
    
            hex = hex * 10;
    
            hex = hex + ( *p & 0x0F );
    
            p++;
    
        }while( --loop != 0 );
    
    }
    

Reply
  • The following code will convert the array of BCD digits in variable bcd to a 32-bit binary value in hex.

    Of course, a 32-bit binary cannot hold the full range of values that you can get from 12 decimal digits. This code just indicates how to go about the conversion.

    main( void )
    {
    
        data unsigned char bcd[6] = { 0x00, 0x00, 0x12, 0x34, 0x56, 0x78 };
        data unsigned char loop;
        data unsigned char data *p;
        data unsigned long hex;
    
        hex = 0;
    
        loop = sizeof( bcd );
    
        p = &bcd[0];
    
        do
        {
            hex = hex * 10;
    
            hex = hex + ( *p >> 4 );
    
            hex = hex * 10;
    
            hex = hex + ( *p & 0x0F );
    
            p++;
    
        }while( --loop != 0 );
    
    }
    

Children