Solved

wot do I copy CString array to char arry

Posted on 2010-09-20
7
871 Views
Last Modified: 2013-11-20
Hi I have a CString array that I want to copy to a char array but im not sure the best way of doing it I have tried the following code but it only copys the last item in the array
CString Protocol_data[48];



Protocol_data[0]="0x00";

Protocol_data[1]="0x01";

for(int i=0;i<20;i++)

{

   Protocol_data[i]="0xFF";



}

//and so on for the Protocol_data[] array";



char m_Textposid[47];

char *charTextPos;

	for(int i=0;i<48;i++){

	charTextPos = Protocol_data[i].GetBuffer(Protocol_data[i].GetLength()) ;

	strcpy_s(m_Textposid,sizeof(m_Textposid), charTextPos);

	

	}

Open in new window

0
Comment
Question by:nchannon
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
Comment Utility
These are 48 strings:
CString Protocol_data[48];

This loop, probably, you wanted to begin from 2:
for(int i=0;i<20;i++)
{
   Protocol_data[i]="0xFF";
}

Because you already set Protocol_data[0] and Protocol_data[1].

This is only one string:
char m_Textposid[47];

This string has 47 characters, but this is only one string. You need 48 strings. For example, so:
char* m_Textposid[48] = { 0 };
for(int i=0;i<48;i++)
{
        m_Textposid[i] = _strdup(Protocol_data[i].GetBuffer(Protocol_data[i].GetLength()) ;
}

_strdup in MSDN: http://msdn.microsoft.com/en-us/library/y471khhc(v=VS.80).aspx

You will need to release the memory in the end:

for(int i=0;i<48;i++)
{
   if (m_Textposid[i])
        free(m_Textposid[i]);
}

If you want to copy all string from the Protocol_data array, you are almost on the right way. The output buffer in this case should be bigger. If each string has 4 characters, so 4 * 48 = 192 and 1 character for 0 in the end. In the loop you need to copy string b y string from the array os CString to the output buffer. Something like:
char m_Textposid[512] = { 0 };
char *charTextPos;
char* p = &m_Textposid[0];
for(int i=0;i<48;i++)
{
        charTextPos = Protocol_data[i].GetBuffer(Protocol_data[i].GetLength()) ;
        strcpy_s(p, strlen(charTextPos), charTextPos);
        p += strlen(charTextPos);
}

Sorry, it's a fast example. I do not know what exactly you need and so I didi not compile it.
0
 
LVL 5

Expert Comment

by:rubikru
Comment Utility
Hello!

It seems to be so, that You are trying to conver HEX in CString (i.e. string "0x00" or "0xFF") for the buffer of the characters with the HEX codes '\x00', '\xFF' and so on.

Than Your code will not do it any way.

You have to first convert CString value (4 characters) to char value (1 character) and than add it to the characters array.

  See the code below.
int GetDigitValue (char digit)
      {
      int asciiOffset, digitValue;
      if (digit >= 48 && digit <= 57)
      {
        asciiOffset = 48;
      }
      else if (digit >= 65 && digit <= 70)
      {
      // digit is 'A' through 'F'
         asciiOffset = 55;
      }
      digitValue = digit - asciiOffset;
      return digitValue;
   }

Open in new window

0
 

Author Comment

by:nchannon
Comment Utility
Hi pgnatyuk thanks for the sample but it dosn't work correctly as it throws an error saying buffer size to small for and seems to be relating to char p as size of char p is 0. I did modify the code and got it to work but i have found where my problem lays and that is I need to use an unsigned char array for my hex values to pass correctly to the serial interface. so if used
unsigned char m_Textposid[] = {0x01,0xff,0xff etc.. } then I get the correct values returning from my serial device.
if I use char m_Textposid[512] = {0} then hex value 0x01 will be returned as = 30
which will not work I need 01 returned.
So my question then is how to I pass the char array values from m_Textposid[0] ...[48] to an unsigned char mychar[] {0x01,0xFF.........} as this will work.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
strcpy_s - that COPIES into the buffer, ie overwrites the previous contents.  You need strcat_s (I've not checked is is called that) to APPEND to the char array.
0
 
LVL 5

Accepted Solution

by:
rubikru earned 500 total points
Comment Utility
So, I got You right :)

Than Your code have to be the following.
See the code section.
int GetDigitValue (unsigned char digit)
      {
      int asciiOffset, digitValue;
      if (digit >= 48 && digit <= 57)
      {
        asciiOffset = 48;
      }
      else if (digit >= 65 && digit <= 70)
      {
      // digit is 'A' through 'F'
         asciiOffset = 55;
      }
      digitValue = digit - asciiOffset;
      return digitValue;
   }


CString Protocol_data[48];

Protocol_data[0]="0x00";
Protocol_data[1]="0x01";
for(int i=0;i<20;i++)
{
   Protocol_data[i]="0xFF";

}
//and so on for the Protocol_data[] array";

unsigned char m_Textposid[48];
unsigned char *ucNum;
	for(int i=0;i<48;i++){
         
         ucNum=(unigned char*)Protocol_data[i].GetBuffer()+2; // get pointer to the first HEX symbol in CString
           m_Textposid[i]=((unsigned char)GetDigitValue(*ucNum))*16;
           ucNum++; // shift pointer to the next HEX symbol
           m_Textposid[i]+=(unsigned char)GetDigitValue(*ucNum);

// now, if You had Protocol_data[i] "0xFF" string, You get '\xFF' symbol in m_Textposid[i] and so on!
        } // for
// now Your m_Textposid[48] has the same values
//  as You would make m_Textposid[] = {0x01,0xff,0xff etc.. } initialization!

Open in new window

0
 
LVL 33

Expert Comment

by:pgnatyuk
Comment Utility
unsigned char mychar[]?
0
 

Author Comment

by:nchannon
Comment Utility
Thanks rubikru that was spot on only had to make a small adjustment to you function GetDigitValue as it didnt cater for lower case a ~ f as when using the CIPAddressCtrl control and then converting the byte values to CString they returned in lowercase hex other than that sweet as man.

int GetDigitValue (char digit)
      {
       int asciiOffset=0;
        int digitValue=0;
      if (digit >= 48 && digit <= 57)
          {
        asciiOffset = 48;
      }
      else if (digit >= 65 && digit <= 70)
      {
      // digit is 'A' through 'F'
         asciiOffset = 55;
      }
        else if (digit >= 97 && digit <= 102)
        {
              // digit is 'a' through 'f'
              asciiOffset = 87;
        }
      digitValue = digit - asciiOffset;
      return digitValue;
   }
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now