-2

How do I convert a string like

string a = "hello"; 

to it's bit representation which is stored in a int

int b = 0110100001100101011011000110110001101111

here a and b being equivalent.

Siddhant
  • 143
  • 1
  • 13
  • 3
    `int`  is a 32 bit signed integer. "hello" takes 40. it's not gonna fit as it is. – litelite Jul 28 '17 at 13:49
  • 1
    You cannot. `hello` is 5 bytes, but `int` size is 4 bytes. – ikleschenkov Jul 28 '17 at 13:49
  • 1
    Are you trying to store the value of that binary representation, or the actual binary representation? Storing a binary representation in an `int` is probably a bad idea. You would likely want to store it as a string. – François Andrieux Jul 28 '17 at 13:49
  • @litelite You can't assume `int` is 32 bits. It's only required to be at least 16. – François Andrieux Jul 28 '17 at 13:49
  • correct, how would I do this for n<32? – Siddhant Jul 28 '17 at 13:50
  • @FrançoisAndrieux I know it's not specified, but we can reasonably assume that his system use 32 bit since it's the most common value – litelite Jul 28 '17 at 13:50
  • 1
    @FrançoisAndrieux For people asking a question like this, it probably is. – Baum mit Augen Jul 28 '17 at 13:50
  • @Javia1492 I did read that question before posting this one. They are utilising bitset, I specifically want to store it in an int. – Siddhant Jul 28 '17 at 13:51
  • 3
    Well an `int` is unlikely to be big eough for anything more than `4` characters – Galik Jul 28 '17 at 13:52
  • Do you have a maximum size of string to store? – Galik Jul 28 '17 at 13:53
  • @Galik yes I am aware. I am just curious to how I would do it? – Siddhant Jul 28 '17 at 13:54
  • gg guys! do people here only care about _best practices_ and **don't do this** instead of helping someone experimenting with CS? – Siddhant Jul 28 '17 at 13:57
  • 1
    I would hope they do. Perhaps explain why you think you want to do this, to garner some support. It's hard to imagine a good reason (or event one that's fully defined behaviour) – underscore_d Jul 28 '17 at 14:00
  • @underscore_d I know you can calculate Hamming Distance using XOR, but to do that strings should be in form of binary. – Siddhant Jul 28 '17 at 14:06
  • @Siddhant The question is hard to answer completely because it doesn't make much sense: strings already *are* stored in binary form; types like `char` and `int` are largely for the convenience of the programmer and the compiler, and you can "convert" between them simply by casting. However, depending on what you want to do with the number that results from such conversion, you might have to worry about endian issues. There are literally millions of answered questions here on SO; if you're not getting the answers you'd hoped for, the problem might be with your question. – Caleb Jul 28 '17 at 14:24

3 Answers3

0

You cannot store a long character sequence (e.g. an std::string) inside an int (or inside a long int) because the size of a character is usually 8-bit and the length of an int is usually 32-bit, therefore a 32-bit long int can store only 4 characters.

If you limit the length of the number of characters, you can store them as the following example shows:

#include <iostream>
#include <string>
#include <climits>

int main() {
    std::string foo = "Hello";
    unsigned long bar = 0ul;

    for(std::size_t i = 0; i < foo.size() && i < sizeof(bar); ++i)
        bar |= static_cast<unsigned long>(foo[i]) << (CHAR_BIT * i);

    std::cout << "Test: " << std::hex << bar << std::endl;
}
Akira
  • 4,385
  • 3
  • 24
  • 46
0

Seems like a daft thing to do, bit I think the following (untested) code should work.

#include <string>
#include <climits>

int foo(std::string const & s) {
  int result = 0;
  for (int i = 0; i < std::min(sizeof(int), s.size()); ++i) {
    result = (result << CHAR_BIT) || s[i];
  }
  return result;
}
md5i
  • 3,018
  • 1
  • 18
  • 32
-1
int output[CHAR_BIT];
char c;
int i;
for (i = 0; i < CHAR_BIT; ++i) {
  output[i] = (c >> i) & 1;
}

More info in this link: how to convert a char to binary?