DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Swift: Master of Decoding Messy JSON
  • Secrets in Code: Understanding Secret Detection and Its Blind Spots
  • From HTTP to Kafka: A Custom Source Connector
  • Making String Search Easier Across Databases

Trending

  • Prompt Injection Is Real, So I Built a Python Firewall for LLM Pipelines
  • Mastering Fluent Bit: Beginners' Guide for Contributing to Our CNCF Project Website
  • Advanced Error Handling and Retry Patterns in Enterprise REST Integrations
  • Observability for Agents and Workflows: Tracing Prompts, Tool Calls, and Business Outcomes End-to-End
  1. DZone
  2. Coding
  3. JavaScript
  4. Converting System Strings Into Standard Strings

Converting System Strings Into Standard Strings

Learn about system strings and check out this solution to a common problem when working with C++/CLI: converting System::string^ into std::string.

By 
Orcun Yilmaz user avatar
Orcun Yilmaz
·
Aug. 27, 17 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
7.1K Views

Join the DZone community and get the full member experience.

Join For Free

C++/CLI (C++ modified for Common Language Infrastructure) is a language specification created by Microsoft and intended to supersede Managed Extensions for C++ that is used in Visual C++. Which means, you can say that C++/CLI is a C++ extension. It is a complete revision that aims to simplify the older Managed C++ syntax, which is now deprecated. So I wanted to use this extension a bit to see what I can do with it.

And I must say that it is awesome to use C++ and also C# features together. It’s like you are writing C++, but when you bored, you can switch into C#.

In this post, I am going to show a solution to the problem of converting System::string^ into std::string. You will face this problem so many times if you decide to use C++/CLI.

What Are System Strings?

System Strings are the types that commonly used in CLR projects. When you made a research on the web, you will see that most of the projects are developed with System::strings^ instead of std::strings.

The difference between System Strings and Standard Strings is about the mapping types into another. In some languages, while you are mapping your variable types into 32 bit, in some languages you can map them into 64 bit. So the fact that using std::string or System::string^ makes no difference is well established.

Is It Still Better to Use String Instead of System.String?

There is no agreed answer on this. It is up to you. For me, I needed to use std::string in my project, so I’ve developed a function to do this. If you needed to use std::string instead of System::string^ like me, you can check out this function:

using System::Runtime::InteropServices::Marshal;

static std::string toStandardString(System::String^ string)
{
 System::IntPtr pointer = Marshal::StringToHGlobalAnsi(string);
 char* charPointer = reinterpret_cast<char*>(pointer.ToPointer());
 std::string returnString(charPointer, string->Length);
 Marshal::FreeHGlobal(pointer);

 return returnString;
}
Strings

Published at DZone with permission of Orcun Yilmaz. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Swift: Master of Decoding Messy JSON
  • Secrets in Code: Understanding Secret Detection and Its Blind Spots
  • From HTTP to Kafka: A Custom Source Connector
  • Making String Search Easier Across Databases

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook