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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:52:00+00:00 2026-05-16T05:52:00+00:00

I convert a character to binary using std::bitset<CHAR_BIT> binary(‘c’); but this doesn’t work for

  • 0

I convert a character to binary using std::bitset<CHAR_BIT> binary('c');
but this doesn’t work for a string,

std::string str = "MyString";
std::bitset<SIZE_OF_STRING_IN_BITS> binary(str); //Error Exception

what should be the alternative?

  • 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-16T05:52:00+00:00Added an answer on May 16, 2026 at 5:52 am

    You could repeat this action over every character of the string, shifting the bitset by CHAR_BIT to the left every time:

    #include <bitset>
    #include <string>
    #include <iostream>
    #include <numeric>
    #include <climits>
    template<size_t N>
    std::bitset<N> string_to_bitset(const std::string& s)
    {
        return accumulate(s.begin(), s.end(), std::bitset<N>(),
               [](const std::bitset<N>& l, char r)
               {
                   return std::bitset<N>(r) | l<<CHAR_BIT;
               });
    }
    int main()
    {
        std::string str = "MyString";
        const size_t SIZE_OF_STRING_IN_BITS = CHAR_BIT * 8;
        std::bitset<SIZE_OF_STRING_IN_BITS> binary = string_to_bitset<SIZE_OF_STRING_IN_BITS> (str);
        std::cout << binary << '\n';
    }
    

    Seeing as the size of the bitset has to be a constant expression, I would go with boost::dynamic_bitset unless this string is a compile-time constant.

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

Sidebar

Related Questions

Using this code to take a string and convert it to binary: bin(reduce(lambda x,
I want to convert a string, using the string class - to Binary. What
I want to convert the first character of a string to Uppercase and the
Possible Duplicate: Convert a number to the shortest possible character string while retaining uniqueness
Is it possible to convert UTF-16 U+610F style character to UTF-8 (hex) E6848F using
I want to convert a String to an array of objects of Character class
I need to convert an integer into a base64-character representation. I'm using OxA3's answer
Can anybody give me some clue that how to convert binary string into a
is there a simple way to convert a character to its binary representation? Im
I am trying to convert a character to its binary representation (so character -->

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.