>> Hex to Decimal 변환 함수(다중문자)

설명 :
char 타입의 Hex문자 여러개를 입력받아 unsigned Decimal 숫자형으로 변환

참조 :
#include <limits>
using namespace std;

함수정의 :
unsigned chr2hex(char *c, int len)
{
    unsigned val=0;
    unsigned val2=0;
    int i;

    for (i=0; i< len; i++)
    {
        if (c[i]>='0' && c[i]<='9') val =  static_cast<unsigned>(c[i] - '0');
        else if (c[i]>='A' && c[i]<='F') val = static_cast<unsigned>(c[i] - 'A')+0xA;
        else if (c[i]>='a' && c[i]<='f') val = static_cast<unsigned>(c[i] - 'a')+0xa;
        else val = std::numeric_limits<unsigned>::max(); // you can also return 0x10 if you prefer.
        val *= ::powl((long)16, (long)(len-i-1));
        val2 += val;
    }

    return val2;
}


사용 예:
unsigned value;

value = chr2hex("A1", 2);
printf("value : 0x%02X, %d \n", value, value);


>>
value : 0xA1, 161

'Code Snippets > Function code' 카테고리의 다른 글

[C]Random Number Generator  (0) 2010.05.14
[C++] Hex to Decimal Convert  (0) 2010.05.14
Posted by 혀나미
,
>> Random으로 숫자를 생성

설명 :
char 포인트 변수에 0~max범위까지의 숫자를 해당 길이 만큼 생성 후 반환
단, 생성된 숫자가 동일하면 새로 생성하여 서로 다른 숫자를 랜덤 생성 함.

참조 :
#include <time.h>

함수정의 :
void rndNumber(char* ret, int max, int length)
{
    int i, j;

    // init rand time
    srand((unsigned)(time(0)));

    // random generator number
    for (i=0; i < length; i++)
    {
        ret[i] = (int)(max*rand()/(RAND_MAX+1.0));

        // check number exists.
        for (j=0; j < i; j++)
        {
            if (ret[i] == ret[j])
            {
                i -= 1;
            }
        }
    }
}


사용 예:
char* tvalue;

tvalue = (char *)malloc(sizeof(char)*10);
rndNumber(tvalue, 15, 10);

for (i = 0; i < 10; i++)
{
    printf("value : %d \n", tvalue[i]);
}

>>
value : 4
value : 8
value : 2
value : 13
value : 10
value : 9
value : 0
value : 12
value : 14
value : 7


'Code Snippets > Function code' 카테고리의 다른 글

[C++] Hex to Decimal Convert (Multi characters)  (0) 2010.05.14
[C++] Hex to Decimal Convert  (0) 2010.05.14
Posted by 혀나미
,
>> Hex to Decimal 변환 함수

설명 :
char 타입의 Hex문자를 unsigned Decimal 숫자형으로 변환

참조 :
#include <limits>
using namespace std;

함수정의 :
unsigned CharToXDigit(char c)
{
    if (c>='0' && c<='9') return static_cast<unsigned>(c-'0');
    else if (c>='A' && c<='F') return static_cast<unsigned>(c-'A')+0xA;
    else if (c>='a' && c<='f') return static_cast<unsigned>(c-'a')+0xa;
    else return std::numeric_limits<unsigned>::max(); // you can also return 0x10 if you prefer.
}


사용 예:
unsigned value;

value = CharToXDigit('A');
printf("value : 0x%02X, %d \n", value, value);


>>
value : 0x0A, 10

'Code Snippets > Function code' 카테고리의 다른 글

[C++] Hex to Decimal Convert (Multi characters)  (0) 2010.05.14
[C]Random Number Generator  (0) 2010.05.14
Posted by 혀나미
,

가변 인수 데이터를 처리하는 예시 문장

 

stdarg.h 헤드파일의 예시 문장

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdarg.h>

int vpf(char *fmt, ...)
{
   va_list argptr;
   int cnt;

   va_start(argptr, fmt);
   cnt = vprintf(fmt, argptr);
   va_end(argptr);

   return(cnt);
}

 

int _tmain(int argc, _TCHAR* argv[])
{
 int inumber = 30;
 float fnumber = 90.0;
 char *string = "abc";

 vpf("%d %f %s\n",inumber,fnumber,string);

 fgetchar();
 return 0;
}

 

(실행 결과)

30 90.000000 abc

Posted by 혀나미
,