Solved

How do i convert between System::String and char* ?

Posted on 2006-10-30
1
245 Views
Last Modified: 2013-11-20
Hello. (Simple newbie questions:)

In VC++ 2005 Express Edition:

1. How do i convert from "String^" to "char*" ?
2. How do i convert from  "char*" to "String^" ?

Thank you.
0
Comment
Question by:Catalin80
1 Comment
 
LVL 48

Accepted Solution

by:
AlexFM earned 50 total points
ID: 17832409
#include "stdafx.h"

using namespace System;
using namespace System::Runtime::InteropServices;

#include <stdio.h>

int main(array<System::String ^> ^args)
{
    // char*" to "String^

    char* p = "abcde";
    String^ s = gcnew String(p);

    Console::WriteLine(s);

    // String^" to "char*
    IntPtr ptr = Marshal::StringToHGlobalAnsi(s);
    char* p1 = (char*)ptr.ToPointer();

    printf(p1);
    Console::WriteLine();

    Marshal::FreeHGlobal(ptr);

    return 0;
}
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question