2008년 1학기 시스템 프로그래밍 과제

 

4. AtoH & HtoA

Write a C program to convert a string of numbers (e.g. “0x3456”)

into a binary hexadecimal. and vice verse.

The length of the string can be arbitrary.

 

 

이것은 앞의 AtoI, ItoA와 비슷하지만,

자리수를 bit operation으로 할 수 있어서 편하게 했습니다.

 

 

코드

/*
*     Ascii_to_Hexa (AtoH)
*
*     This function is converting from string(ASCII) to Hexadecimal number.
*
*     - Argument
*     input_str : user input text(string)
*
*     - return
*     unsigned int : converted from text to Hexadecimal number.
*
*
*     It resembles Ascii_to_Integer(AtoI) function.
*     Only difference is Hexa is 0-15 and it can be calculated bit-level operation
*
*
*     HexaDecimal is 0-15. So it represented 0-9A-F.
*     and also A = a, B = b, C = c, D = d, E = e, F = f. Therefore I write about small letter.
*
*     HexaDecimal can be calculated bit-level operation.
*     Shift the 4 bit then it changed cipher
*     (e.g. 0x0F : 0000 1111, 0x0F << 4 -> 1111 0000 = 0xF0)
*
*
*     Made by Bak JinYeong, 2008.03.13*/
unsigned int Ascii_to_Hexa(char* input_str)
{
    int for_loop_i = 0; // integer variable use loop for loop sentence
    int str_size = My_strlen(input_str); // length of input_str
    unsigned int result_value = 0; // unsigned int variable use result value. It intialize 0.

    if('0' == input_str[0] && 'x' == input_str[1]) // 0x prefix represent Hexadecimal number. So Check first
    {
        for_loop_i = 2; // If true, then I check number except 0x
    }
    else
    {
        return 0; // input_str is not Hexadecimal format. So return 0.
    }

    for(; for_loop_i < str_size ; for_loop_i++) // looping length of input_str string.
    {
        if('0' <= input_str[for_loop_i] && input_str[for_loop_i] <= '9') // if input_str's byte is in '0' to '9'
        {
            result_value += (input_str[for_loop_i] - '0') << 4 * (str_size - for_loop_i - 1); // get character and convert to number using (- '0'). and set cipher.
        }
        else if('A' <= input_str[for_loop_i] && input_str[for_loop_i] <= 'F') // if input_str's byte is in 'A' to 'F'
        {
            result_value += (input_str[for_loop_i] - 'A' + 10) << 4 * (str_size - for_loop_i - 1); // get character and convert to number using ( - 'A' + 10). and set cipher.
        }
        else if('a' <= input_str[for_loop_i] && input_str[for_loop_i] <= 'f') // if input_str's byte is in 'a' to 'f'
        {
            result_value += (input_str[for_loop_i] - 'a' + 10) << 4 * (str_size - for_loop_i - 1); // get character and convert to number using ( - 'a' + 10). and set cipher.
        }
        else
        {
            return 0; // Other character is not Hexadecimal format.
        }   
    }

    return result_value; // return
}

/*
*
*     Hexa_to_Ascii (HtoA)
*
*     This function is converting from Hexadecimal number to string(ASCII)
*
*     - Argument
*     input_str : user input text(string)
*
*     - return
*     unsigned int : converted from text to Hexadecimal number.
*
*
*     It resembles Integer_to_Ascii(ItoA) function.
*     Only difference is Hexa is 0-15 and it can be calculated bit-level operation.
*     HexaDecimal is 0-15. So it represented 0-9A-F.
*
*     HexaDecimal can be calculated bit-level operation.
*     So If I want to get nibble anywhere,
*     first shift the variable until that nibble reach to least significant nibble,
*
*     and &(AND) operation with 0x0f.
*     (e.g. input_int : 1000 1001 1010 1011. I want to get 1001.
*     input_int >> 8 -> 0000 0000 1000 1001
*     (input_int >> 8) & 0x0f -> 0000 0000 1000 1001 & 0000 0000 0000 1111 = 0000 0000 0000 1001)
*
*
*     Made by Bak JinYeong, 2008.03.13*/    
void Hexa_to_Ascii(unsigned int input_int, char* output_str)
{
    int for_loop_i = 0; // integer variable use loop for loop sentence
    char each_byte = 0; //

    output_str[0] = '0'; // 0x prefix represent Hexadecimal number.
    output_str[1] = 'x'; // 0x prefix represent Hexadecimal number.

    for(for_loop_i = 2; for_loop_i < BUS_SIZE_NIBBLE + 2 ; for_loop_i++) // looping 8 times because 32bit is 0x12345678.
    {
        each_byte = (input_int >> (4 * (BUS_SIZE_NIBBLE - for_loop_i + 1))) & 0x0f; // get one nibble from left to right.
        if(each_byte < 10) // HexaDecimal is 0-15. So it represented 0-9A-F.
        {
            output_str[for_loop_i] = each_byte + '0'; // 1 -> '1', '1' = 1 + '0' in ASCII CODE
        }
        else
        {
            output_str[for_loop_i] = each_byte + 'A' - 10; // 11 -> 'B', 'B' = 11 + 'A' - 10 in ASCII CODE
        }
    }
    output_str[for_loop_i] = '\0'; // Least significant byte of string must have \0.
}

/*
*     My_strlen
*
*     This function is returned length of argument string.
*
*     - Argument
*     Arg_Str : This function calculate length of this string.
*
*    - Return
*    int : length of string
*
*    
*     looping until meet \0. That is end of string.
*
*
*    Made by Bak JinYeong, 2008.03.17*/
int My_strlen(char* Arg_Str)
{
    int length_of_string = 0; // length of String. It initialize 0.
    while('\0' != Arg_Str[length_of_string]) // loop until meet \0. That is end of String.
    {
        length_of_string++; // add 1 lenth of string.
    }

    return length_of_string; // return length of string.
}

크리에이티브 커먼즈 라이선스
Creative Commons License

글에 잘못된 점, 다른 점, 부족한 점이 있다면 지적해주세요.
댓글, 트랙백, 메일 모두 고맙습니다.

트랙백 주소 :: http://nosyu.pe.kr/trackback/1456

댓글을 달아 주세요

[로그인][오픈아이디란?]