By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,412 Members | 1,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,412 IT Pros & Developers. It's quick & easy.

converting from system::string^ to char* problems

P: 2
I am having a problem with the following code:
Expand|Select|Wrap|Line Numbers
  1. System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
  2.      String^ texts = textBox1->Text;
  3.      char *text = strToChars(texts);
  4.      String^ shiftcs = domainUpDown1->Text;
  5.      char *shiftc = strToChars(shiftcs);
  6.      int shift = atoi(shiftc);
  7.      char *cipher = encCCipher(text, shift);
  8.      String^ ciphers = gcnew String(cipher);
  9.      textBox2->Text = ciphers;
  10. }
  11. ...
  12. char* strToChars(System::String^ str) {
  13.      msclr::interop::marshal_context ^ context = gcnew msclr::interop::marshal_context();
  14.      const char* chars = context->marshal_as<const char*>(str);
  15.      return (char*)chars;
  16. }
  17. ...
  18. char* encCCipher (char *plain, int shift) {
  19.     char lowerAlphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  20.     int len = strlen(plain) ;
  21.     char *cipher = plain;
  22.     char cchar = NULL;
  23.     int ccode = 0;
  24.     for (int i = 0; i < len; i++) {
  25.         cchar = tolower(plain[i]);
  26.         ccode = NULL;
  27.         for (int j = 0; j < 26; j++) {
  28.             if (cchar == lowerAlphabet[j]) {
  29.                 ccode = j;
  30.                 break;
  31.             }
  32.         }
  33.         if (ccode != NULL){
  34.             cipher[i] = lowerAlphabet[(ccode + i) % 26];
  35.         } else {
  36.             cipher[i] = cchar;
  37.         }
  38.     }
  39.     return cipher;
  40. }
  41.  
Entering "Testing cipher" into textbox1 puts out "tfuwmsm krzsqe."
After pressing the button the variables have the following in them:
texts: "Testing cipher"; text: 0x007B6B78 "tfuwmsm krzsqe"; shiftcs: "0"; shiftc: 0x007B6BC8 "0"; shift: 0; cipher: 0x007B6B78 "tfuwmsm krzsqe"; ciphers: "tfuwmsm krzsqe"
So I'm pretty sure the problem is in the converting the input from the textbox to a char* array.
And if you are wondering, this is a Caesar cipher program.
This is c++ code compiled on visual c++ express 2008 on vista.
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: 2
Never mind, I figured it out. The problem was in the for loop.

Expand|Select|Wrap|Line Numbers
  1. char* encCCipher (char *plain, int shift) {
  2.     char lowerAlphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','  m','n','o','p','q','r','s','t','u','v','w','x','y'  ,'z'};
  3.     int len = strlen(plain) ;
  4.     char *cipher = plain; //should be "char *cipher = new char; strcpy(cipher, plain);"
  5.     char cchar = NULL;
  6.     int ccode = 0;
  7.     for (int i = 0; i < len; i++) {
  8.         cchar = tolower(plain[i]);
  9.         ccode = NULL;
  10.         for (int j = 0; j < 26; j++) {
  11.             if (cchar == lowerAlphabet[j]) {
  12.                 ccode = j;
  13.                 break;
  14.             }
  15.         }
  16.         if (ccode != NULL){
  17.             cipher[i] = lowerAlphabet[(ccode + i) % 26]; //"i" should be "shift" 
  18.         } else {
  19.             cipher[i] = cchar;
  20.         }
  21.     }
  22.     return cipher;
  23. }
Jun 29 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.