Solved

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

Posted on 2006-10-30
1
243 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
iSeries FTP Exit Program 8 114
sameEnds challenge 3 106
How to convert MFC::CString to UTF8 wchar_t* 10 96
firstChar challenge 13 84
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now