#include "MTool.h"
int CMTool::BytesToIntLittleEndian(const byte *buf, int length) {
/*
// buf : 箭磊啊 little endian栏肺 扁废登绢 乐绰 官捞呈府狼 矫累 林家.
// length : 箭磊狼 辨捞
// return value : 函券等 搬苞蔼
*/
int r = 0, i;
for ( i = 0 ; i < length; ++i ) {
r += ( buf[i] & 0xff ) }
return r;
}
void CMTool::IntToBytesLittleEndian(int n, byte *buf, int BufferLen) {
/*
// n : 镜妨绰 箭磊
// buf : 箭磊啊 little endian栏肺 扁废瞪 官捞呈府狼 矫累 林家.
// bufferLen : buf 俊辑何磐 啊侩 啊瓷茄 农扁
*/
int i;
int toI;
if ( BufferLen == -1 ) {
toI = getByteSize(n);
} else {
toI = BufferLen;
}
i = 0;
while( true ) {
buf[i] = (byte)(n & 0xff);
n = n >> 8;
i++;
if ( i == toI )
break;
}
}
int CMTool::getByteSize(int n) {
int r = 0;
do {
r++;
n = n >> 8;
} while ( n != 0 );
return r;
}
uint32 CMTool::ntohl(uint32 n) {
//return ( ( n & 0xff ) 8 ) + ( ( n & 0xff000000 ) >> 24 );
return ( ( n & 0xff ) 8 ) + ( ( n & 0xff000000 ) >> 24 );
}
int CMTool::atoi(const byte *buf, int len) {
int i;
int tot = 0;
int n = 1;
int s = 0;
if ( buf[0] == '-' )
s = 1;
for ( i = len-1; i >= s; --i ) {
tot += (buf[i]-'0')*n;
n *= 10;
}
return (s==0? tot: -tot);
}