Avatar of howardsd
howardsd asked on

CString w/ hex values > byte array

I have a CString with hex values "0122C24D".  I need to create a byte array where each index of the array corresponds to each of the 2 respective hex characters in the CString.

arr[0]=0x01
arr[1]=0x22
arr[2]=0xC2
arr[3]=0x4D

So far I have the code below, but blows up on the strcat_s.

char *szMyString = NULL;
char szWrite[512];
CString cs = "0122C24D", tmp;

for(int i=0; i<cs.GetLength(); i+=2)
{
    tmp = cs.Mid(i,2);
    szMyString = (char*) (LPCTSTR) tmp;
    strcat_s(szWrite,512,szMyString);
}

Any ideas?
System ProgrammingC++

Avatar of undefined
Last Comment
anmalaver

8/22/2022 - Mon
anmalaver

Hi

Avoid using strcat, use a temporary CString and += operator, to avoid problems with unicode and multibyte.
ASKER
howardsd

how does using a CString += operator help me?  I'm trying to create a byte array.
ASKER CERTIFIED SOLUTION
anmalaver

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
howardsd

had to change the szWrite to a BYTE array rather than a char array.  but after that, it works like a charm.

i knew it would something simple.

thnx!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
anmalaver

You're welcome...
anmalaver

m...
About my points?