Click here to Skip to main content
15,879,490 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I have below function


C++
void StrToByte2(LPCTSTR str, BYTE *dest)
{
	UINT count = _ttoi(str);
	BYTE buf[4] = { 0 };
	char string[10] = { 0 };
	sprintf_s(string, 10, "%04d", count);
	for (int i = 0; i < 4; ++i)
	{
		if ((string[i] >= '0') && (string[i] <= '9'))
			buf[i] = string[i] - '0';
	}
	dest[0] = (BYTE)(buf[0] << 4) | buf[1];
	dest[1] = (BYTE)(buf[2] << 4) | buf[3];
}


If i call this function on "1234" ( any digits) , dest output some 12814!

C++
struct st               
{
    byte    btID[2];
    int     nID;
};

PTR ptr(new st);
StrToByte2(strCode, ptr->btID);


but when i call this function on any hexadecimal ex A123 , it outputs 0000 always.

Below function is used to convert back the dest code to str

C++
CString Byte2ToStr(const byte* pbuf)
{
    CString str;
    str.Format(_T("%02X%02X"), pbuf[0], pbuf[1]);
    return str;
}


How can i get A123 to converted to bytes and than back to str to display A123??

Please help!!

What I have tried:

Tried below function but on convert back to str ,some i cannot see A123, but insted some random number like 1348

C++
void StrToByte2(LPCTSTR str, BYTE *dest)
{
	BYTE buf[4] = { 0 };
	char string[10] = { 0 };
	sprintf_s(string, 10, "%04d", str);
	for (int i = 0; i < 4; ++i)
	{
		if ((string[i] >= '0') && (string[i] <= 'F'))
			buf[i] = string[i] - '0';
	}
	dest[0] = (BYTE)(buf[0] << 4) | buf[1];
	dest[1] = (BYTE)(buf[2] << 4) | buf[3];
}
Posted
Updated 31-Oct-18 11:47am
Comments
KarstenK 31-Oct-18 14:32pm    
Solution 1 is correct. Because these conversion are common tasks a lot of function are available. Use them, because they are proofed and tested code and are secure even in edge cases. ;-)
Rick York 31-Oct-18 18:43pm    
If your numbers have typing prefixes like 0x for hexadecimal and 0 for octal then strtoul and _tcstoul can handle those. In fact, it takes a base as an argument so you can give it binary or any other base you want to.

As suggested, use the (generic-text variant of) strtol:
C++
void StrToByte2(LPCTSTR str, BYTE * dest)
{
	LONG u16 = _tcstol( str, NULL, 16);
	dest[0] = (BYTE) (u16 >> 8);
	dest[1] = (BYTE) (u16);
}



or at least, write a sensible hand-crafted one. Try

C++
void StrToByte2Alt(LPCTSTR str, BYTE dest[])
{
	for (size_t b=0; b<2; ++b)
	{
		dest[b] = 0;
		for (size_t nb=0; nb<2; nb++)
		{
			dest[b] <<= 4;
			TCHAR tc = str[2*b+nb];
			if ( tc >= _T('0') && tc <= _T('9') )
				dest[b] |= tc - _T('0');
			else if ( tc >= _T('A') && tc <= _T('F') )
				dest[b] |= tc - _T('A') + 10;
			else if ( tc >= _T('a') && tc <= _T('f') )
				dest[b] |= tc - _T('a') + 10;
			else
			{// throw all the throwable here...
			}
		}
	}
}


or, using C++ streams:
C++
#ifdef UNICODE
    typedef std::wistringstream InputStringStream;
#else
    typedef std::istringstream InputStringStream; 
#endif // UNICODE

BOOL strtobyte2(LPCTSTR str, std::array<BYTE, 2> & b)
{
	UINT16 u16;
	InputStringStream iss(str);
	iss >> hex >> u16;
	if ( ! iss) return FALSE; 
	b[0] = static_cast<BYTE>(u16 >> 8);
	b[1] = static_cast<BYTE>(u16);
	return TRUE;
}
 
Share this answer
 
#include <cstdlib>

int main()
{
    char * p = NULL;
    long n = strtol( "abcd", & p, 16 );
    printf("%ld\r\n", n);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900