Solved

wot do I copy CString array to char arry

Posted on 2010-09-20
7
896 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
ID: 33718787
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
ID: 33718922
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
ID: 33724922
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 33726289
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
ID: 33727139
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
ID: 33728988
unsigned char mychar[]?
0
 

Author Comment

by:nchannon
ID: 33729637
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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.

863 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

23 Experts available now in Live!

Get 1:1 Help Now