MFCQuery
2006-08-03 13:45:59 UTC
Hi,
Unsigned char has more range than char. In case of unicode there is only
TCHAR. no unsigned TCHAR.
Actually i have a unicode project and i have a generic function to convert
integer into array of its hex values.
template <class T> CString VariableToHexString(T t)
{
CString ret, tmp;
for(int i = sizeof(T)-1; i>=0; --i)
{
TCHAR c = reinterpret_cast<TCHAR*>(&t)[i];
tmp.Format(_T("%02hX"), c);
ret+=tmp;
}
return ret;
}
and i call it like
int sval=10;
CString d1=VariableToHexString<int>(sval);
I should use above or below.
template <class T> unsigned char* VariableToHexString(T t)
{
unsigned char *ret=(char*)calloc(1,sizeof(T)+1);
char bytes[2];
memset(bytes,0,2);
for(int i = sizeof(T)-1; i>=0; --i)
{
unsigned char c = reinterpret_cast<unsigned char*>(&t)[i];
sprintf(bytes,"%02hX",c);
ret[i]=bytes[0];
}
return ret;
}
In my ansi project actual function was.
template <class T> CString VariableToHexString(T t)
{
CString ret, tmp;
for(int i = sizeof(T)-1; i>=0; --i)
{
unsigned char c = reinterpret_cast<unsigned char*>(&t)[i];
tmp.Format("%02hX", c);
ret+=tmp;
}
return ret;
}
Please suggest.
Unsigned char has more range than char. In case of unicode there is only
TCHAR. no unsigned TCHAR.
Actually i have a unicode project and i have a generic function to convert
integer into array of its hex values.
template <class T> CString VariableToHexString(T t)
{
CString ret, tmp;
for(int i = sizeof(T)-1; i>=0; --i)
{
TCHAR c = reinterpret_cast<TCHAR*>(&t)[i];
tmp.Format(_T("%02hX"), c);
ret+=tmp;
}
return ret;
}
and i call it like
int sval=10;
CString d1=VariableToHexString<int>(sval);
I should use above or below.
template <class T> unsigned char* VariableToHexString(T t)
{
unsigned char *ret=(char*)calloc(1,sizeof(T)+1);
char bytes[2];
memset(bytes,0,2);
for(int i = sizeof(T)-1; i>=0; --i)
{
unsigned char c = reinterpret_cast<unsigned char*>(&t)[i];
sprintf(bytes,"%02hX",c);
ret[i]=bytes[0];
}
return ret;
}
In my ansi project actual function was.
template <class T> CString VariableToHexString(T t)
{
CString ret, tmp;
for(int i = sizeof(T)-1; i>=0; --i)
{
unsigned char c = reinterpret_cast<unsigned char*>(&t)[i];
tmp.Format("%02hX", c);
ret+=tmp;
}
return ret;
}
Please suggest.