C++ | Chuck Norris Unary Code

Hello peeps! Lord Hypersonic greets you. Today I am presenting you a C++ program to Encode a text into Chuck Norris Unary Code and Decode Chuck Norris Unary Code to its original text.
You have to tell the program what you want to do? You want to encrypt a text or want to decrypt the ciphertext.
After that tell the type of ASCII Encoding of the message i.e., 7-bit or 8-bit. Now enter the message or ciphertext which you want to encode or decode.

You must be wondering what the hell is this Chuck Norris Unary Code? If this is the case, then let us do some discussion on this topic. If you know what is Chuck Norris Unary Code, then you can skip to the algorithm.

About Chuck Norris Unary Code

This is similar to binary numbers, but the difference is that in binary we have a sequence of 0s and 1s, but in Unary code, we have a sequence of 0s only
First, we convert text to binary code. Let us Take "A". 
So binary code of "A" is 01000001
Now, we separate the sequence of 0s and 1s.
For example: 
In the first sequence, we have one 0.
In the second sequence, we have one 1.
In the third sequence, we have five 0s.
And in the fourth sequence, we have one 1.

We have to change the sequence of 0 with "00 " with the number of 0s, and the sequence of 1 will be replaced with "0 " followed by 0s with the number of 1.
For example:- 
In the first sequence: "00 0" (For one 0).
In the second sequence: "0 0" (For one 1).
In the third sequence: "00 00000" (For five 0).
In the fourth sequence: "0 0" (For one 1).

Hence Unary code of  "A"  will be : 00 0 0 0 00 00000 0 0

Hope it is clear what is Chuck Norris Unary Code. 
Now let us discuss the algorithm.

Algorithm

We have two parts in this program. One part is encrypting text to the unary message and the second is decrypting unary to text. We will discuss the algorithm for both parts one by one.

Encrypting

First, ask for the message after that ask for the type of ASCII Encoding (7-bit OR 8-bit).
If  ASCII encoding is of 7-bit then binary code generated will be of 7-bit, If ASCII encoding is of 8-bit then binary code generated will be of 8-bit. 

Now generate a binary code of the message according to the ASCII encoding provided by the user and save the binary code in a string variable.

Declare a string variable, let's say "unaryMessage" and make it equal to empty string ( unaryMessage = ""; )

Start a loop from i = 0  to i < the length of binary.
    if binary[i] == '1' then:-
        unaryMessage = unaryMessage + "0 ";
        start a loop from j = i  till binary[j] == '1'
            increase the value of i by 1 ( i++; ).
            Add 0 at the end of unaryMessage ( unaryMessage.push_back('0'); ).
    After the second loop is finished, add space at the end of unaryMessage ( unaryMessage.push_back(' '); ).
    if binary[i] == '0' then:-
        unaryMessage = unaryMessage + "00 ";
        start a loop from j = i till binary[j] == '0'
            increase the value of i by 1 ( i++; ).
            Add 0 at the end of unaryMessage ( unaryMessage.push_back('0'); ).
    After the second loop is finished, add space at the end of unaryMessage ( unaryMessage.push_back(' '); ).

After the first loop is finished, the unary code of the message is stored in the string unaryMessage.

This is how you can encode a simple text to Chuck Norris Unary Code.

Decrypting

First ask for the ciphertext and type of ASCII encoding (i.e., 7-bit or 8-bit).
Now remove any extra space from the end of the message.
After removing extra space from the end of the string, count number of spaces present in between the message string.

Now declare an integer variable sizeOfAr and initialize it with the number of spaces + 1 ( int sizeOfAr = noOfSpaces + 1; ).
Add space at the end of the message ( message.push_back(' '); ).
Declare an array of string Ar with length sizeOfAr ( string Ar[sizeOfAr] ).

Split the message from space and save the strings in the array of string "Ar".

Now we have different sequences of 0s in array Ar. Now we have to convert it into binary code.
For this task, we will need a string to store the binary numbers, ( string binary = ""; ).

Start a loop from i = 0 to i < sizeOfAr :-
    if i is divisible by 2 then:-
        increase the value of i by 1 ( i++; );
        if Ar[i-1] is equal to "0"  then:-
            Add the number of 1 at the end of binary as the size of Ar[i].
        if Ar[i-1] is equal to "00" then:-
            Add Ar[i] at the end of binary.

This will convert the unary code to binary.
Now we just have to convert the binary code to string according to the ASCII encoding provided by the user.

This is how you can decrypt Chuck Norris Unary Code to a simple text.

Source Code

/*
Program: Chuck Norris Unary Message
Description: Program to convert text to unary message and unary message to text
Author: Lord Hypersonic
Email: lordhypersonic.522@gmail.com
Website: www.lordhypersonic.blogspot.com
*/
#include <iostream>
#include <string>
#include <bitset>
#include <sstream>
#include <ctype.h>
#include <conio.h>

using namespace std;

//function to encrypt message to Chuck Norris Unary Message
string ChuckNorrisMessageEncrypt(string message, int bit = 7)
{
    string binary = "",unaryMessage = "";
    if (bit == 7) //if ASCII coding is 7bit
    {
        for (int i = 0; i < message.size(); i++)
        {
            binary += bitset<7>(message[i]).to_string(); //convert message to 7bit binary message
        }
    }
    if (bit == 8) // if ASCCI coding is 8bit
    {
        for (int i = 0; i < message.size(); i++)
        {
            binary += bitset<8>(message[i]).to_string(); //convert message to 8bit binary message
        }
    }
    for (int i = 0; i < binary.size();)
    {
        if (binary[i] == '1')
        {
            unaryMessage += "0 ";
            for (int j = i; binary[j] == '1'; j++)
            {
                i++;
                unaryMessage.push_back('0');
            }
            unaryMessage.push_back(' ');
        }
        if (binary[i] == '0')
        {
            unaryMessage += "00 ";
            for (int j = i; binary[j] == '0'; j++)
            {
                i++;
                unaryMessage.push_back('0');
            }
            unaryMessage.push_back(' ');
        }
    }
    return unaryMessage;
}

//function to Decrypt message from Chuck Norris Unary text to simple text.
string ChuckNorrisMessageDecrypt(string message, int bit = 7)
{
    for (int i = message.size() - 1; i >= 0; i--) //removing extra space from the end of string.
    {
        if (message[i] != ' ')
            break;
        message.resize(message.size()-1);
    }
    int noOfSpaces = 0;
    for (int i = 0; i < message.size(); i++) //counting number of spaces in the message.
    {
        if (message[i] == ' ')
            noOfSpaces++;
    }
    int sizeOfAr = noOfSpaces + 1;
    message.push_back(' ');
    string Ar[sizeOfAr],token;
    int pos = 0, ArIndex = 0;
    //splitting message from space and saving the strings in array of string.
    while ((pos = message.find(' ')) != string::npos)
    {
        token = message.substr(0,pos);
        Ar[ArIndex] = token;
        message.erase(0, pos + 1);
        ArIndex++;
    }
    string binary = "";
    //converting chuck norris text to binary
    for (int i = 0; i < sizeOfAr; i++)
    {
        if (i%2 == 0)
        {
            i++;
            if (Ar[i-1] == "0")
            {
                int len = Ar[i].size();
                string temp = Ar[i];
                for (int j = 0; j < len; j++)
                {
                    temp[j] = '1';
                }
                binary += temp;
            }
            if (Ar[i-1] == "00")
            {
                binary += Ar[i];
            }
        }
        else continue;
    }
    //converting binary to simple text
    string decodedMessage = "";
    stringstream in(binary);
    if (bit == 7) //if encoding of 7bit
    {
        bitset<7> bs;
        while(in >> bs)
        {
            decodedMessage.push_back(bs.to_ulong());
        }
    }
    if (bit == 8) //if encoding of 8bit
    {
        bitset<8> bs;
        while(in >> bs)
        {
            decodedMessage.push_back(bs.to_ulong());
        }
    }
    return decodedMessage;
}

int main ()
{
    string message,choice,BITS;
    while (1)
    {
        cout<<"Select action:- \n1. Encrypt message \n2. Decrypt message \n>> "; getline(cin,choice);
        for (int i = 0; i < choice.size(); i++)
        {
        if (isupper(choice[i]))
                tolower(choice[i]);
        }
        if (choice == "1" || choice == "encrypt" || choice == "encrypt message")
        {
            cout<<"ASCII Encoding:- \n1. 7-BIT \n2. 8-BIT \n>> "; getline(cin,BITS);
            for (int i = 0; i < BITS.size(); i++)
            {
                if (isupper(BITS[i]))
                    tolower(BITS[i]);
            }
            if (BITS == "1" || BITS == "7" || BITS == "7-bit" || BITS == "7  bit" || BITS == "7-bits" || BITS == "7 bits")
            {
                cout<<"Enter message to encrypt: "; getline(cin, message);
                cout<<"Encoded message: "<<ChuckNorrisMessageEncrypt(message,7)<<endl;
            }
            else if (BITS == "2" || BITS == "8" || BITS == "8-bit" || BITS == "8 bit" || BITS == "8-bits" || BITS == "8 bits")
            {
                cout<<"Enter message to encrypt: "; getline(cin, message);
                cout<<"Encoded message: "<<ChuckNorrisMessageEncrypt(message,8)<<endl;
            }
            else continue;
        }
        else if (choice == "2" || choice == "decrypt" || choice == "decrypt message")
        {
            cout<<"ASCII Encoding:- \n1. 7-BIT \n2. 8-BIT \n>> "; getline(cin,BITS);
            for (int i = 0; i < BITS.size(); i++)
            {
                if (isupper(BITS[i]))
                    tolower(BITS[i]);
            }
            if (BITS == "1" || BITS == "7" || BITS == "7-bit" || BITS == "7  bit" || BITS == "7-bits" || BITS == "7 bits")
            {
                cout<<"Enter cipher-text to decrypt: "; getline(cin, message);
                cout<<"Decoded message: "<<ChuckNorrisMessageDecrypt(message,7)<<endl;
            }
            else if (BITS == "2" || BITS == "8" || BITS == "8-bit" || BITS == "8 bit" || BITS == "8-bits" || BITS == "8 bits")
            {
                cout<<"Enter cipher-text to decrypt: "; getline(cin, message);
                cout<<"Decoded message: "<<ChuckNorrisMessageDecrypt(message,8)<<endl;
            }
            else continue;
        }
        else continue;
        _getch();
    }
    return 0;
}


No comments

Powered by Blogger.