Solved

wot do I copy CString array to char arry

Posted on 2010-09-20
7
960 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

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…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Suggested Courses

752 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