472,989 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 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. }
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 3713
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

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...
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....
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...
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
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...
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...
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...
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...
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: ...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.