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 344755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:01:12+00:00 2026-05-12T11:01:12+00:00

I have the following string: index 0 1 2 3 4 5 6 7

  • 0

I have the following string:

index                                       0   1   2   3   4   5   6   7
std::string myString with the content of "\xff\xff\xff\x00\xff\x0d\x0a\xf5"

When I’m refering to myString[3], I get the expected ‘\x00’ value.

But when I’m referring to myString[5], I get two values “\x0d\x0a” instead of just ‘\x0d’.

Even more interesting is the myString[6] value, which is the ‘\xf5’. This time it’s like the \x0d didn’t exist and the correct position was referenced.

My question would be: what is so special about the \x0d character in a std:string object? How come it is skipped when indexing? It’s like counting this way:

index                     0   1   2   3   4   5   5   6
std::string myString = "\xff\xff\xff\x00\xff\x0d\x0a\xf5"

As a comment, the ‘\x0d’ character is the 13th ASCII character “carriage return” and ‘\x0a’ is the line feed character.

UPDATE: Can it be that std::string considers “\x0d\x0a” as a single character and thus occupies only one position in the string? Is this ‘\x0d’ a “mystery” character with regard to std::string?

ADDITIONAL INFO: http://en.wikipedia.org/wiki/Newline

  • 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-05-12T11:01:13+00:00Added an answer on May 12, 2026 at 11:01 am

    Are you sure this is happening with std::string? std::string::operator[] returns a const char &, so how can it be returning two chars ('\x0d' and '\x0a')?

    That said, "\x0d\x0a" is usually used for line endings under Windows, whereas only '\x0a' is used under Linux, so conversion of the former to the latter is relatively common under Windows — for example, I’m thinking of the behaviour of fopen when called with "wt". I would guess something similar is happening to you.

    Edit: Based on your comments on the original question, I think I can guess what’s going on.

    I believe your string doesn’t really contain what you think it contains. You’re being misled because the mechanism you’re using to output the string to a file (probably ofstream?) is performing end-of-line translation. This means that a '\n' (the Unix end-of-line code) is being translated to '\r\n' (the Windows end-of-line code). The purpose of end-of-line translation is to make code more portable between operating systems. You can inhibit it by opening the file in binary mode; for ofstream, this is done by specifying the ios_base::binary flag when you open the file, but this flag is not set by default.

    (See this Wikipedia article for more information on end-of-line markers on different operating systems.)

    This is what I believe is going on. Your string actually contains

    index                 0   1   2   3   4   5   6
    myString contents  "\xff\xff\xff\x00\xff\x0a\xf5"
    

    You’re outputting it something like this:

    ofstream file("myfile.txt");
    for(size_t i=0; i<myString.size(); i++)
        ofstream << myString[i];
    

    Because of the end-of-line translation expalined above, the '\x0a' in myString[5] is being output as '\x0d\x0a', and that’s what is confusing you.

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

Sidebar

Related Questions

I have the following C++ code std::map<std::string, std::vector<std::vector<std::vector<double> > > > details details[string][index][index].push_back(123.5); May
I have a long string and a list of [end-index, string] like the following:
I have following html in string and i have to extract the content only
I have the following function: public ActionResult Index(string caller_id, int? id) { Now I
I have following string: Test, User < test@test.com >, Another, Test < another@test.com >,
I have following situation: String a = A Web crawler is a computer program
I have the following string in a variable. Stack Overflow is as frictionless and
I have the following string which will probably contain ~100 entries: String foo =
I have the following String First Last <first.last@email.com> I would like to extract first.last
I have the following string: <div id=mydiv>This is a div with quotation marks</div> I

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.