Discussion:
TCHAR and unsigned char
(too old to reply)
MFCQuery
2006-08-03 13:45:59 UTC
Permalink
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.
MFCQuery
2006-08-04 04:09:09 UTC
Permalink
Hi, If i use TCHAR c = reinterpret_cast<TCHAR*>(&t)[i];
then i don't get right value. But i can't use TCHAR c =
reinterpret_cast<unsigned char *>(&t)[i];

If i want to make this unicode function(i.e. i want to use CString) then
please suggest what to do.
Post by MFCQuery
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.
Mihai N.
2006-08-04 07:27:31 UTC
Permalink
Kind of unrelated, but:

template <class T> CString VariableToHexString(T t)

This tells me T can be anything.
but because:
for(int i = sizeof(T)-1; i>=0; --i)
TCHAR c = reinterpret_cast<TCHAR*>(&t)[i];

this means T can only be an array.
Not even a pointer, because then sizeof(T)-1 is crap.
But passing an array as parameter, on stack, might not be the smartest thing.

I would really drop the generic parameter here, the function as implemented
is not quite generic, so there is no gain (except you being forced to twist
the type system with risky casts).
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Ted
2006-08-04 14:22:09 UTC
Permalink
_TUCHAR is what I use

Ted.
Post by MFCQuery
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.
MFCQuery
2006-08-05 07:03:13 UTC
Permalink
Thanks.
Post by Ted
_TUCHAR is what I use
Ted.
Post by MFCQuery
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.
Continue reading on narkive:
Loading...