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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Writing DTOs With Java8, Lombok, and Java14+
  • Formatting Strings in Java: String.format() Method
  • Improving Serialization and Memory Efficiency With a LongConverter
  • Hibernate Validator vs Regex vs Manual Validation: Which One Is Faster?

Trending

  • AI-Based Threat Detection in Cloud Security
  • Performance Optimization Techniques for Snowflake on AWS
  • The Modern Data Stack Is Overrated — Here’s What Works
  • Scalable System Design: Core Concepts for Building Reliable Software
  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
6.6K 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

  • Writing DTOs With Java8, Lombok, and Java14+
  • Formatting Strings in Java: String.format() Method
  • Improving Serialization and Memory Efficiency With a LongConverter
  • Hibernate Validator vs Regex vs Manual Validation: Which One Is Faster?

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!