2010-04-04 3 views

답변

4
void hexconvert(char *text, unsigned char bytes[]) 
{ 
    int i; 
    int temp; 

    for(i = 0; i < 4; ++i) { 
     sscanf(text + 2 * i, "%2x", &temp); 
     bytes[i] = temp; 
    } 
} 
+0

감사합니다. 그것은 일이다. – hlgl

2

문자열을 16 진수로 정수로 구문 분석하려는 것처럼 들립니다. C++ 방식 :

#include <iostream> 
#include <sstream> 
#include <string> 

template <typename IntType> 
IntType hex_to_integer(const std::string& pStr) 
{ 
    std::stringstream ss(pStr); 

    IntType i; 
    ss >> std::hex >> i; 

    return i; 
} 

int main(void) 
{ 
    std::string s = "DEADBEEF"; 
    unsigned n = hex_to_integer<unsigned>(s); 

    std::cout << n << std::endl; 
}