470,612 Members | 2,506 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,612 developers. It's quick & easy.

converting from system::string^ to char* problems

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
1 3425
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.

Similar topics

7 posts views Thread by AlexFarokhyans | last post: by
4 posts views Thread by Yan Vinogradov | last post: by
15 posts views Thread by Yifan | last post: by
24 posts views Thread by Marcus Kwok | last post: by
2 posts views Thread by Alejandro Aleman | last post: by
2 posts views Thread by =?Utf-8?B?QWJoaW1hbnl1IFNpcm9oaQ==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.