util.cpp
上传用户:tt_chan
上传日期:2009-12-03
资源大小:4523k
文件大小:1k
- #include "stdafx.h"
- #include "util.h"
- int _hextodec( char *hex, int len )
- {
- int dec = 0;
- int base = 1;
- int index;
- for ( int i = len - 1; i >= 0; i-- )
- {
- if ( hex[i] >= '0' && hex[i] <= '9' )
- index = hex[i] - '0';
- else
- index = hex[i] - 'A' + 10;
- dec += index * base;
- base *= 16;
- }
- return dec;
- }
- // 16柳荐 沥荐甫 16柳荐 巩磊肺 函券矫挪促.
- static char _hexdectochar( char num )
- {
- if ( num < 10 )
- return num + '0';
-
- return (num - 10) + 'A';
- }
- void _dectohex( int dec, char *hex, int len )
- {
- int index = 0;
- while ( index < len )
- {
- hex[index++] = _hexdectochar( dec % 16 );
- dec = dec / 16;
- if ( dec == 1 )
- {
- if ( index < len )
- hex[index++] = _hexdectochar( dec );
- break;
- }
- else if ( dec == 0 )
- {
- if ( index < len )
- hex[index++] = '0';
- break;
- }
- }
- // 芭操肺 历厘等 16柳荐 巩磊凯阑 第笼绰促.
- for ( int i = 0; i < index / 2; i++ )
- _swap( hex[i], hex[index - i - 1] );
- }