Solved

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

Posted on 2006-10-30
1
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

707 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