About Me

My photo
Mostly software programming related blogs.

Friday, May 1, 2015

Convert integer from one base to another

Given an integer from any base 2 to 16, convert it into another base 2 to 16. So Binary can be converted to hex, hex to octal or decimal to hex or hex to base 3, hexadecimal to base 8 etc etc..

Program/Code is in C/C++/linux/g++

#include <iostream>
#include <inttypes.h>
#include <math.h>

using namespace std;

void convertbase(char *inp, uint16_t inp_base, char *out, uint16_t out_base);
bool validateinp(char *inp, uint16_t inp_base);

main()
{

        char inp[64] = {'\0'};
        char out[64] = {'\0'};

        uint16_t inp_base, out_base;
        cout << "enter the base of input number (between 2 to 16): ";
        cin >> inp_base;

        if (inp_base < 2 || inp_base > 16) {
                cout << "output base not valid." << endl;
                return -1;
        }  

        cout << "enter input number: ";
        cin >> inp;

        bool isInpValid = validateinp(inp, inp_base);

        if (!isInpValid) {
                cout << "input num is not valid." << endl;
                return -1;
        }  

        cout <<"enter the base of output number (between 2 to 16): ";
        cin >> out_base;

        if (out_base < 2 || out_base > 16) {
                cout << "output base not valid." << endl;
                return -1;
        }  

        convertbase(inp, inp_base, out, out_base);   
}

bool validateinp(char *inp, uint16_t base) {
        char *ch = inp;
        uint32_t val;
        while (*ch != '\0') {
                val = (*ch > '9') ? ((*ch >= 'a') ? (*ch - 'a' + 10) : (*ch - 'A' + 10)) : (*ch - '0');
                if (val >= base) {
                       return false;
                }
                if (!((*ch >= 0 && *ch <= '9') || (*ch >= 'A' && *ch <= 'F' ) || (*ch >= 'a' && *ch <= 'f'))) {
                        return false;
                }
                ch++;
        }
        return true;
}

void convertbase(char *inp, uint16_t inp_base, char *out, uint16_t out_base)
{
        uint32_t val = 0, r = 0, i = 0, decimal = 0;
        char ch1, *ch = inp;

        while (*ch != '\0') {
                val = (*ch > '9') ? ((*ch >= 'a') ? (*ch - 'a' + 10) : (*ch - 'A' + 10)) : (*ch - '0');
                decimal = (decimal * inp_base) + val;
                ch++;
        }

        while (decimal > 0) {
                r = decimal % out_base;
                ch1 = (r >= 10) ? (r - 10 + 'A') : ('0' + r);
                out[i++] = ch1;
                decimal = decimal / out_base;
        }
        out[i] = '\0';

        for (r = 0; r < i/2; r++) {
                ch1 = out[r];
                out[r] = out[i - r - 1];
                out[i - r - 1] = ch1;
        }

        cout << "output is " << out << endl;
}

Thursday, April 30, 2015

strtok in C/C++ Program - Code implementation - string tokenizer

#include<iostream>
#include <inttypes.h>
#include <string.h>
#include <stdio.h>

using namespace std;

char* my_strtok(char *inp, char *delim);

main()
{
        char src[128] = {'\0'};
        char delim[16] = {'\0'};

        char *out;

        cout << "enter input string src: ";
        gets(src);
        cout << "enter input string dilimiter: ";
        gets(delim);

        cout << "src is " << src << endl;
        cout << "delimiter is " << delim << endl;

        out  = my_strtok(src, delim);  

        while(out != NULL) {
                cout << "token is " << out << endl;
                out = my_strtok(NULL, delim);
        }  
}

char *my_strtok(char *inp, char *deli) {

        static char *start = NULL;
        char *ch = NULL, *delim = NULL, *ret = NULL;
        uint32_t num_chars = 0;

        if (inp != NULL) {
                start = inp;
        }  

        if (start == NULL || deli == NULL) {
                return NULL;
        }  

        ch = ret = start;

        while (*ch != '\0') {
                delim = deli;
                while(*delim != '\0') {
                        if (*ch == *delim) {
                                *ch = '\0';
                                if (num_chars > 0) {
                                        start = ch+1;
                                        return ret;
                                } else {
                                        ret = ch+1;
                                        break;
                                }
                        }
                        delim++;
                }
                ch++;
                num_chars++;
        }
        if (*ch == '\0') {
                start = NULL;
                if (inp == NULL && strlen(ret) != 0) {
                        return ret;
                }
        }
        return NULL;
}

Thursday, March 12, 2015

UEFI vs legacy BIOS

Technical Note: UEFI BIOS vs. Legacy BIOS, Advantech(EN)

https://www.youtube.com/watch?v=dRMIvY7BiL4

System BIOS

BIOS Session 1 - System Memory Map

https://www.youtube.com/watch?v=4hr1aXf2ark 

BIOS Session 2 - Legacy BIOS

https://www.youtube.com/watch?v=D4SESHBT6C0

BIOS Session 3 - HIgh Level Overview of the BOOT flow

https://www.youtube.com/watch?v=dzEoSLfbFuA


Wednesday, March 11, 2015

UEFI

UEFI Specifications:

http://www.uefi.org/specifications

EDK Project page

http://www.tianocore.org/

Training material:

Online Training:
“Defining Specifications’ Role in Firmware” and Lessons 1-5 of “Implementing the Boot Process.” http://tianocore.sourceforge.net/wiki/UEFI_EDKII_Learning_Dev
I Recommend Downloading the Zip File** for each of the lessons

Latest Training Material:
–             Contains source code & documents needed for labs
–             Creates C:\FW sub-directory automatically
–             Follow the _UEFI_Lab_Guide.pdf for getting started


Getting Started Writing Application:


Use Visual Studio command prompt to compile or Run.

Mailing List:


It has a good archive. Search archive is very helpful.

EDK2 Documents:


Intel UEFI course page including UEFI shell:



Basic instructions for using EFI - Whitepaper - Intel


Installing windows in UEFI mode



UEFI HII Training (Intel, July 2013)