본문 바로가기
WORK/Sotfware

HexChar To Int

by KANG Stroy 2013. 2. 22.
728x90
728x90


hex로 되어 있고...char의 형태를 가진 값을 int의 형태로 변경하기 위해서 다음과 같이 ... 함수를 만들어도 ...되고..


#define II(x)           _II(x)

#define _II(x)          L ## x


int HexCharToInt(CHAR c)

{

if ((c >= II('0')) && (c <= II('9')))

return c - II('0');

if ((c >= II('a')) && (c <= II('f')))

return c - II('a') + 10;

if ((c >= II('A')) && (c <= II('F')))

return c - II('A') + 10;

return -1;

}


VC에서는 .... 


strtol의 함수를 사용하여 쉽게 변경을 할 수 있습니다... 


아래의 예제는... http://www.cplusplus.com/reference/cstdlib/strtol/ 여기서 퍼 왔습니다. ^^ 그리고 약간의 수정을 하였습니다. 


long int strtol (const char* str, char** endptr, int base);


Convert string to long integer

Parses the C-string str interpreting its content as an integral number of the specified base, which is returned as along int value. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax that depends on the base parameter, and interprets them as a numerical value. Finally, a pointer to the first character following the integer representation in str is stored in the object pointed by endptr.

If the value of base is zero, the syntax expected is similar to that of integer constants, which is formed by a succession of:
An optional sign character (+ or -)
An optional prefix indicating octal or hexadecimal base ("0" or "0x"/"0X" respectively)
A sequence of decimal digits (if no base prefix was specified) or either octal or hexadecimal digits if a specific prefix is present

If the base value is between 2 and 36, the format expected for the integral number is a succession of any of the valid digits and/or letters needed to represent integers of the specified radix (starting from '0' and up to 'z'/'Z' for radix 36). The sequence may optionally be preceded by a sign (either + or -) and, if base is 16, an optional "0x" or"0X" prefix.

If the first sequence of non-whitespace characters in str is not a valid integral number as defined above, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

For locales other than the "C" locale, additional subject sequence forms may be accepted.?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* strtol example */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* strtol */

int main ()
{
  char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
  char * pEnd;
  long int li1, li2, li3, li4;
  li1 = strtol (szNumbers,&pEnd,10);  // 10진수
  li2 = strtol (pEnd,&pEnd,16);       // 16진수 바로 이것을 사용하면 되겠군요 ㅎㅎ 
  li3 = strtol (pEnd,&pEnd,2);
  li4 = strtol (pEnd,NULL,0);
  printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", li1, li2, li3, li4);
  return 0;
}


   Output:


The decimal equivalents are: 2001, 6340800, -3624224 and 7340031



728x90

'WORK > Sotfware' 카테고리의 다른 글

PIC 인터럽트 설정  (0) 2013.06.11
warning: control reaches end of non-void function  (0) 2013.06.11
CFileFolderDialog class  (0) 2013.02.05
VC c++ 6.0 버튼에 이미지 넣기  (0) 2013.02.04
응용개발자의 자료저장소  (0) 2013.02.01

댓글