Converting Case using Binary and Hexadecimal values

We can use the same logic for octal numbers. Also using the binary logic below we can convert lower case letters to upper case letters. For reference, Chapter 1 of the book Digital logic ( Author M. Morris Mano ) may be helpful.


Capital to Small Case Using Binary Values:

/**
 * @author  Quickgrid ( Asif Ahmed )
 * @link    https://quickgrid.wordpress.com
 * Problem: Usage of Binary Values
 */

#include<cstdio>

int main () {
    /*
     * We represent binary ( Base 2, 0 and 1 ) with prefix, 0b
     */
    register unsigned i = 0b0;

    /*
     * ASCII code for capital A
     * Explanation:
     *  1*2^6 + 0*2^5 + 0*2^4 + 0*2^3 + 0*2^2 + 0*2^1 + 1*2^0
     * =   64 +     0 +     0 +     0 +     0 +     0 +     1
     * =   65
     */
    int A = 0b1000001;

    /*
     * ASCII code for line feed
     */
    int n = 0b0001010;

    /*
     * The trick here is that difference between An ASCII Code for a capital
     * and small letter is always 32. So if we can toggle that bit representing
     * 32 then we can convert capital to small letter and vice versa.
     */
    while (i < 0b0011010){
        /*
         * Since Capital letter differ in the bit representing 32 bit
         * We can just bitwise OR it with any capital letter and get
         * its small letter representation.
         * We know from truth table of OR that
         * 0 OR 1 = 1, meaning our bit representing 32 is active
         */
        int a = 0b0100000 | A;

        /*
         * Here A starts from 'A' and goes to 'Z'
         * Here n represents new line
         */
        printf("Capital: %c%c", A++, n);
        printf("Small: %c%c", a, n);

        ++i;
    }

    return 0;
}

 

Small and Capital Case Using Hexadecimal Values:

/**
 * @author  Quickgrid ( Asif Ahmed )
 * @link    https://quickgrid.wordpress.com
 * Problem: Usage of Hexadecimal Values
 */

#include<cstdio>

int main () {
    /*
     * We represent hexadecimal ( Base 16, from 0 to F total of 16 ) with prefix, 0x
     */
    register unsigned i = 0x0;

    /*
     * ASCII code for capital A
     * Explanation:
     *  6*16^1 + 1*16^0
     * =    96 +      1
     * =    97
     */
    int A = 0x61;

    /*
     * ASCII code for line feed
     */
    int n = 0xA;
    int small_a = 0x41;

    while (i < 0x1A){
        int a = small_a++;

        /*
         * Here A starts from 'A' and goes to 'Z'
         * Here n represents new line
         */
        printf("Small: %c%c", A++, n);
        printf("capital: %c%c", a, n);

        ++i;
    }

    return 0;
}

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s