473,326 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 3756
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

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

Similar topics

27
by: Trep | last post by:
Hi there! I've been having a lot of difficult trying to figure out a way to convert a terminated char array to a system::string for use in Visual C++ .NET 2003. This is necessary because I...
7
by: AlexFarokhyans | last post by:
Hello, I'm trying to convert String to Char array. I'm getting a string from user input text box and then I have char firstName. I need to convert the string that is in the text box to firstName....
4
by: Yan Vinogradov | last post by:
There are two ways to convert a char* received from the unmanaged code into a managed System.String object that I am aware of. The first one is to simply construct String object passing char* as...
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
24
by: Marcus Kwok | last post by:
Hello, I am working on cleaning up some code that I inherited and was wondering if there is anything wrong with my function. I am fairly proficient in standard C++ but I am pretty new to the .NET...
2
by: Alejandro Aleman | last post by:
Hello! i know this may be a newbie question, but i need to convert a string from System::String^ to char*, in the msdn page tells how, but i need to set to /clr:oldSyntax and i dont want it...
6
by: DaTurk | last post by:
Hi, I have several interfaces in CLI that I access via c#. My problem is, is that down in the unmanaged c++, which the CLI lies on top of, I have a lot of c_str() happening. But all of my...
2
by: =?Utf-8?B?QWJoaW1hbnl1IFNpcm9oaQ==?= | last post by:
Hi, I am using Visual C++ in Visual Studio 2005 to create a Managed Wrapper around some C++ LIBS. I've created some classes that contains a pointer to the LIB classes and everthing seems to...
1
by: Vivienne | last post by:
Hi, I am using a c# library in my c++ project. so I have to convert a basic string to System::string to make use of a function that takes System::String as argument. I did it like this: ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.