[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1029
  • Last Modified:

wot do I copy CString array to char arry

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
nchannon
Asked:
nchannon
  • 2
  • 2
  • 2
  • +1
1 Solution
 
pgnatyukCommented:
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
 
rubikruCommented:
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
 
nchannonAuthor Commented:
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
AndyAinscowFreelance programmer / ConsultantCommented:
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
 
rubikruCommented:
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
 
pgnatyukCommented:
unsigned char mychar[]?
0
 
nchannonAuthor Commented:
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now