unionBase.cpp 3.33 KB
//base.h 实现功能基本的函数
/* 
 *  Copyright (c) 2011,  广州江南科友union  
 *  All rights reserved. 
 *  文件名称: unionBase.cpp
 *  摘 要:    实现功能基本的函数  
 *  当前版本: 1.0,  编写者: 杨武,修改时间: 2011-11-22 修改内容: 创建
 *  
 */
#include "stdafx.h"
#include "unionBase.h"

CString GetRandStr(int count)
{
	int num = 0 ;
	char temp[2] = {0};
	CString str=_T("");
	for(int i =0 ;i<count;i++)
	{
		num = rand()%9 + 1;
		memset(temp,0,1);
		sprintf_s(temp, "%d", num); 
		str += CString(temp);
	}
	return str;
}


char * my_strcat(char * _Dest,char * _Source,int &_dLen,int _sLen)
{
	char *pD,*pS;
	pD = _Dest;
	pS = _Source;
	pD = pD + _dLen;
	for(int i=0;i<_sLen;i++)
	{
		*pD = *pS;
		pD++;
		pS++;
	}
	*pD = 0;
	_dLen = _dLen+_sLen;
	return _Dest;
}

int my_strncut(char * str_dest,char * str_source,int start_position, int len)  
{  
	int i=start_position;
	int j=0;
	for(j=0;j<len;j++)  
	{  
		str_dest[j]=str_source[i];  
		i++;  
	}
	return  len;
}


void IntToChar(int iValue,int number,char *buf)
{
	CString str;
	str.Format(_T("%d"),iValue);
	int j = str.GetLength();
	for(int i=0;i<(number - j );i++)
	{
		str = _T("0") + str; 
	}
	memcpy(buf,(char*)(LPCTSTR)str,number);
	return;
}

void IntToHexChar(int iValue,int number,char *buf)
{
	CString str;
	str.Format(_T("%0X"),iValue);
	int j = str.GetLength();
	for(int i=0;i<(number - j );i++)
	{
		str = _T("0") + str; 
	}
	memcpy(buf,(char*)(LPCTSTR)str,number);
	return;
}

void lenToChar(const int len,char * keyLenType)
{
	char keyLen[3] = {0};
	switch(len)
	{
	case 16:
		keyLen[0] = '0';
		keyLen[1] = '1';
		break;
	case 32:
		keyLen[0] = '0';
		keyLen[1] = '2';
		break;
	case 48:
		keyLen[0] = '0';
		keyLen[1] = '3';
		break;
	default:
		keyLen[0] = '0';
		keyLen[1] = '2';
	}
	memcpy(keyLenType,keyLen,2);
}



int revValue(char * value)
{
	CString strRev ;
	int rev = 0;
	strRev = CString(value);
	strRev.TrimLeft(_T("0"));
	if(!strRev.IsEmpty())
	{
		rev = 0 - _ttoi(strRev);
	}
	return rev;
}

int HexToDec(char* s)
{
	char*p = s;
    if(*p=='\0')
		return 0;
	while(*p=='0')
		p++;
    int dec = 0;
    char c;
    while(c=*p++)
	{
		dec<<=4;
		if(c>='0'&&c<='9')
		{
			dec+=c-'0';
			continue;
		}

        if(c>='a'&&c<='f')
        {
            dec+=c-'a'+10;
            continue;
        }

        if(c>='A'&&c<='F')
        {
            dec+=c-'A'+10;
            continue;
        }
        return -1;
	}

    return dec;
}

long HexToLDec(char* s)
{
	char*p = s;
    if(*p=='\0')
		return 0;
	while(*p=='0')
		p++;
    long dec = 0;
    char c;
    while(c=*p++)
	{
		dec<<=4;
		if(c>='0'&&c<='9')
		{
			dec+=c-'0';
			continue;
		}

        if(c>='a'&&c<='f')
        {
            dec+=c-'a'+10;
            continue;
        }

        if(c>='A'&&c<='F')
        {
            dec+=c-'A'+10;
            continue;
        }
        return -1;
	}

    return dec;
}


int getConfig(struct CONFIG *cfg)
{
	cfg->hsmLenOfMsgHeader = GetPrivateProfileInt(_T("hsminfo"),_T("hsmmsgheadlen"),0,_T(".\\config.ini")); 
	cfg->port = GetPrivateProfileInt(_T("hsminfo"),_T("port"),8,_T(".\\config.ini")); 
	cfg->isLenOfHsmMsg = GetPrivateProfileInt(_T("hsminfo"),_T("isLenOfHsmMsg"),0,_T(".\\config.ini"));
	cfg->timeout = GetPrivateProfileInt(_T("hsminfo"),_T("timeout"),3000,_T(".\\config.ini")); 
	GetPrivateProfileString(_T("hsminfo"),_T("ip"),_T("127.0.0.1"),cfg->ip,25,_T(".\\config.ini"));
	return 0;
}