Solved

wot do I copy CString array to char arry

Posted on 2010-09-20
7
977 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 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