Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8097913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:54:42+00:00 2026-06-05T21:54:42+00:00

What I want to do is to convert a string into const char*. Ideally

  • 0

What I want to do is to convert a string into const char*.

Ideally I could just use “const char* myConstChar = myString.c_str()” but as my example below shows; it doesn´t work well for binary data:

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>

using namespace std;

int main(){
    string myString;  // This need to be string
    ifstream infile;
const char* myConstChar;  // This need to be const char*

infile.open("binary.bin");

if(infile){
        while(getline(infile, myString)){ 
        std::cout << "Data: " << myString << " string length: "; 
            std::cout << myString.length() << "\n";
            myConstChar = myString.c_str();
            std::cout << "const char Data: " << myConstChar; 
            std::cout << " const char length: "<< strlen(myConstChar) <<"\n";
        }
    }

    infile.close();
    return 0;
}

This returns “string length: 13” and “const char length: 3”.

Apparently there are some loss of data when converting string to const char* using myString.c_str()!

How do I convert a string to const char* without losing binary data?!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:54:44+00:00Added an answer on June 5, 2026 at 9:54 pm

    This is almost certainly because your binary data contains zero-valued bytes. These are identical to null-terminators, which functions like strlen use to determine the end of a string.

    It’s arguable that arbitrary binary data shouldn’t be treated as a string. So use std::vector<char> instead, and don’t use functions like strlen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert all texts in a string into html entities but preserving
I want to convert a string like 2012-03-08T00:00:00+01:00 into Date.. I tried to use
I want to convert a string into a date, this is simple. But what
I want to convert a NSString into a const char * in order to
i want to convert this string into DateTime. Tue Aug 19 15:05:05 +0000 2008
I want to convert a C-style string into a byte-vector. A working solution would
i am trying to convert a LPCSTR string into LPCTSTR string. i want to
I want to convert an OutputStream into a String object. I am having an
I have a string as in aa_bb_cc. I want to convert it into a
I want to convert String to Date in different formats. For example, I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.