가끔 자료를 출력 할 때에 주어지는 포멧에 따라서
소수점을 만들어 줘야 하는 경우가 있다.

예를 들면 5.1203 을
0x05 0x0C 0x03
이런식으로 한다고 할때...



그런데 저것을 받아서 출력하면 뒤에 0 이 들어가야 하는 곳은..
기본 값으로 0을 채워서 줘야 되겠지?

그럴 경우에는 어떻게 하면 될까?

에서 힌트를 얻어서 해보았다.

char test[5] = {0x03, 0x10, 0x02, 0x05, 0x06};
printf("%d.%02d%02d%02d%02d\n", test[0], test[1], test[2], test[3], test[4]);

printf 할 때에...
%0숫자d 를 해 주면 숫자만큼의 자릿수로 무조건 쓰게 된다.
숫자가 없을 경우에는 0 을 채워서.

출력은
3.16020506
Press any key to continue
이렇게 나오더라는.

'Study.. > Programming' 카테고리의 다른 글

system time 측정하기.  (0) 2010.04.07
이러니 될 일이 없었던거다..;;;;  (0) 2010.01.14
WinCE Emulator 설치시 오류...  (0) 2009.10.27
문제가 있을 때는...  (0) 2009.08.06
IAR 에서 printf 사용하기.  (0) 2009.06.30
Posted by Yoons...
,
이전에 post 중에
에 보면 서로 경과 시간을 출력하는 것이 있다.
( gettimeofday 사용)
이번에는 pda 의 응용프로그래밍 때문에 pda 에서도 유사한 코드를 이용, 출력하도록 해 보았다.

을 참조  하였다.


#include <time.h>

static __inline int isLeapYear(int year)
{ return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0); }

static __inline int getCountOfLeapYearFor(int year)
{ return (year / 4) - (year / 100) + (year / 400); }

static __inline int getCountOfDaysFor(int year)
{ return (365 * year) + getCountOfLeapYearFor(year); }

INT64 MC_knlCurrentTime()
{
static const int daysInMonths[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
SYSTEMTIME st[1];
INT64 totalDays;
int month0Based, i;

GetSystemTime(st);


totalDays = st->wDay - 1; // start with 1
month0Based = st->wMonth - 1; // start with 1
for ( i=0; i<month0Based; ++i )
totalDays += daysInMonths[i];
if ( isLeapYear(st->wYear) && month0Based > 1 ) // passed leap month?
++totalDays;
totalDays += getCountOfDaysFor(st->wYear - 1);
return ((totalDays * 24 + st->wHour) * 60 + st->wMinute) * 60 + st->wSecond  - 62135596800i64/*=1970-01-01*/;
}
참고로 코드 수정 부분은 INT64 형으로 바꾼것,
(마지막 함수의 return 형태와 totalDays 변수. )
그리고 ms 단위로 하지 않고 sec 단위로 하기 위하여 마지막 수식을 살짝 바꿨다.

그리고 실제 사용은 다음과 같이 하였다.

INT64 time = MC_knlCurrentTime();

data[size++] = (unsigned char)(time >> 24);
data[size++] = (unsigned char)(time >> 16);
data[size++] = (unsigned char)(time >> 8);
data[size++] = (unsigned char)time;
(작성환경 : VS2008, 실행환경 : PDA. AoM-1250, MIPS processor, WinCE 5.0)

참고로 원 글을 참조하자면, WinCE 5.0 에서는 위 식에서 ms 단위까지의 계산을 지원하지 않는다고 한다. 6.0 부터는 지원한다고 하지만...
(정작 내 폰은 이미 WinCE 6.5 이다. 내 폰(100만원) 보다 안 좋은 180만원짜리 PDA 같으니라고..;; )

그리고 계산상 4byte 이면 32bit 를 표현할 수 있고, 아직 한참 남아돌만큼 쓸 수 있으니 걱정은 하지 않도록 하자.

더불어 이 시간 계산이 맞는지 궁금하면 다음과 같이 linux 에서 컴파일 하여 작동해 보면 확인할 수 있다.

#include <sys/time.h>
#include <iostream.h>

int main()
{
        struct timeval cur;
        gettimeofday(&cur, 0);
cout << "now : " << cur.tv_sec<<endl;
        return 0;
}
참~~ 쉽죠잉?
(환경 : 컴파일러는 다음 참조하자. 뭐 별꺼 있겠냐만. ;;;
yoons@yoons-desktop:~$ g++ -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-targets=all --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu4)
yoons@yoons-desktop:~$
Posted by Yoons...
,