티스토리 툴바

   개발자 로니강(Ronie.Kang)  
Front Page
Tag | Location | Media | Guestbook | Admin   
 
Hex String to integer

이전에 포스팅한 GetProfileToString을 통해서, 특정 값을 읽어왔는데 제기럴 Hex 값이었다.
Int 값이었으면 atoi를 이용해서 int로 쓰겠는데 왠걸 hex string... 열심히 구글링 해보니 CodeProject에 Sample이 있어 업어왔다.


linux에서 왜 string.h에 strupr이 없는걸까???????
보안에 문제가 있는 API라서? 쩝....

Set로 움직이는 함수입니다.
꼭 필요한 분들이 있을 것 같습니다.

수정사항이 있으시면 언제든지!!!  피드백 부탁 드립니다.

고운하루 되세요


char* StrUpr( char *str )
{
    int loop = 0;
    while( str[loop] != '\0' )
    {
        str[loop] = (char) toupper( str[loop] );
        loop++;
    }
    return str;
}


/*
 * hex string to int
*/

int xtoi(char *value)
{
    struct CHexMap
    {
        char chr;
        int value;
    };

    const int HexMapL = 16;
    CHexMap HexMap[HexMapL] =
    {
        {'0', 0}, {'1', 1},
        {'2', 2}, {'3', 3},
        {'4', 4}, {'5', 5},
        {'6', 6}, {'7', 7},
        {'8', 8}, {'9', 9},
        {'A', 10}, {'B', 11},
        {'C', 12}, {'D', 13},
        {'E', 14}, {'F', 15}
    };

    /* remove space */
    int len = strlen(value);
    for(int i = 0; i < len; i++) {
        if( *(value+i) == ' ') {
            value++;
        } else {
            break;
        }
    }

    /* alloc buffer and change upper */
    char *mstr = StrUpr(strdup(value));
    char *s = mstr;
    int result = 0;
    if (*s == '0' && *(s + 1) == 'X')
        s += 2;
    bool firsttime = true;
    while (*s != '\0')
    {
        bool found = false;
        for (int i = 0; i < HexMapL; i++)
        {
            if (*s == HexMap[i].chr)
            {
                if (!firsttime) result <<= 4;
                result |= HexMap[i].value;
                found = true;
                break;
            }
        }
        if (!found) break;
        s++;
        firsttime = false;
    }
    free(mstr);
    return result;
}

크리에이티브 커먼즈 라이선스
Creative Commons License
Tag : , , , , ,
Commented by kuaaan at 2009/10/15 19:53  r x
strtol, strtoul 함수를 사용하세요. 세번째 인자에 16을 주면 16진수로 변환됩니다. atoi는 내부적으로 strtol에 세번째 인자로 10을 주어 재호출하는 것으로 알고 있습니다.
http://linux.die.net/man/3/strtol

name    password    homepage
 hidden


BLOG main image
개발자 로니의 스토리.... 삽질, 날샘, 야근, 개발, 자취생, 일상, 프로그래밍 개발, 등등등....
 Notice
 Category
분류 전체보기 (191)
매일매일갱생 (76)
아이폰 (0)
Books (3)
Programming (86)
BroadCast (12)
Personal Projects (6)
유용한 프로그램 (0)
비공개 (0)
 TAGS
torrent SIP 메모리 누수 가카 벨소리 변경 sip 해킹 malloc 조중동 C++ VirtualBox Porter-Duff iPhone usb 인식 실수 스마트폰 warning Memory Leak 벨소리 인터넷전화 arduino uno 국내 토렌토 myLG070 directfb itunes 동기화 C iTunes 바보짓 빵꾸똥꾸 아이폰 It 아이튠즈 iphone 개발환경 토렌토 Linux 미라지 M480 강남역 MB ubuntu 용자
 Calendar
«   2012/02   »
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      
 Recent Entries
Finding Memory Leaks Using t.. (2)
Program Arduino with AVR-GCC
새로운 장난감....
ATMEGA328P-PU
Arduino uno 간단한 보드 설명.
퇴근길 강남역에서 만난 도인?.. (1)
xper 5월 정기모임이 일정 (1)
국내 토렌토 주소 모음
미라지(SKT) M480 벨소리 변경..
myLG070 sip 추출하기 (3)
warning: braces around scala..
 Recent Comments
331-707 충청남도 천..
331-707 충청남도 천안시 서북구 성환읍 대학로 91번지 - 02/17
I think this is bes..
Thesis Writing Services - 02/15
great post nice work
Dissertation Help - 02/14
java/lang/Object j..
sumin - 2011
Common persons do n..
Research paper - 2011
At the essay writin..
buy essay - 2011
The hard working pe..
dissertation service - 2011
Pretty good text re..
buy essay - 2011
Yeah absolutely ve..
resume writing service - 2011
You know that you h..
buy a research paper - 2011
In fact, some stude..
Term Paper Online - 2011
 Recent Trackbacks
COFDM 방식에서의 Bi..
Steve Yoon's log
다중포인터 읽기
.
 Archive
2011/09
2010/06
2010/05
2010/04
2010/02
2010/01
2009/12
2009/11
2009/10
2009/09
 Link Site
zextor
괴짜 프로그래머의 일상사~@@
Gag & Peace, and more..
Kazakhstan Almaty.......
Min-A
Sadgarret
Steve Yoon's log
가슴 뛰는 삶을 살아라
오스틴 파워
GUI sin
melanie parker_Lady
제레미의 TV 2.0 이야기..
 Visitor Statistics
Total : 85,600
Today : 47
Yesterday : 59
rss