Domanda

Come stampare la rappresentazione po 'di una stringa

std::string = "\x80";

void print (std::string &s) {

    //How to implement this
}
È stato utile?

Soluzione

avrei votato per bitset:

void pbits(std::string const& s) { 
    for(std::size_t i=0; i<s.size(); i++) 
        std::cout << std::bitset<CHAR_BIT>(s[i]) << " "; 
} 

int main() {
    pbits("\x80\x70"); 
}

Altri suggerimenti

Little-endian o big-endian?

for (int i = 0; i < s.length(); i++)
    for (char c = 1; c; c <<= 1) // little bits first
        std::cout << (s[i] & c ? "1" : "0");
for (int i = 0; i < s.length(); i++)
    for (unsigned char c = 0x80; c; c >>= 1) // big bits first
        std::cout << (s[i] & c ? "1" : "0");

Da quando ho sentito un po 'di lamentarsi portabilità di supponendo che un char è un byte a 8 bit nei commenti delle altre risposte ...

for (int i = 0; i < s.length(); i++)
    for (unsigned char c = ~((unsigned char)~0 >> 1); c; c >>= 1)
        std::cout << (s[i] & c ? "1" : "0");

Questa è scritto dal punto di vista molto C-ish ... se si sta già utilizzando C ++ con STL, si potrebbe anche andare fino in fondo e sfruttare le funzionalità STL bitset invece di giocare con le stringhe.

Prova:

#include <iostream>

using namespace std;

void print(string &s) {
  string::iterator it; 
  int b;

  for (it = s.begin(); it != s.end(); it++) {
    for (b = 128; b; b >>= 1) {
      cout << (*it & b ? 1 : 0); 
    }   
  }
}

int main() {
  string s = "\x80\x02";
  print(s);
}

ampliando la risposta di Stephan202:

#include <algorithm>
#include <iostream>
#include <climits>

struct print_bits {
    void operator()(char ch) {
        for (unsigned b = 1 << (CHAR_BIT - 1); b != 0; b >>= 1) {
            std::cout << (ch & b ? 1 : 0); 
        }
    }
};

void print(const std::string &s) {
    std::for_each(s.begin(), s.end(), print_bits());
}

int main() {
    print("\x80\x02");
}

soluzione più semplice è prossimo:

const std::string source("test");
std::copy( 
    source.begin(), 
    source.end(), 
    std::ostream_iterator< 
        std::bitset< sizeof( char ) * 8 > >( std::cout, ", " ) );
  • Alcune implementazioni STL consentono std :: setBase () manipolatore per la base 2.
  • Si potrebbe scrivere il proprio manipolatore se vuole soluzione più flessibile di quello esistente.

Modifica
Ops. Qualcuno ha già pubblicato soluzione simile.

Mi dispiace Ho segnato questo come un duplicato. In ogni caso, per fare questo:

void printbits(std::string const& s) {
   for_each(s.begin(), s.end(), print_byte());
}

struct print_byte {
     void operator()(char b) {
        unsigned char c = 0, byte = (unsigned char)b;
        for (; byte; byte >>= 1, c <<= 1) c |= (byte & 1);
        for (; c; c >>= 1) cout << (int)(c&1);
    }
};

Se si vuole farlo manualmente, si può sempre utilizzare una tabella di ricerca. 256 valori in una tabella statica è certo un sacco di spese generali:

static char* bitValues[] = 
{
"00000000",
"00000001",
"00000010",
"00000011",
"00000100",
....
"11111111"
};

Poi la stampa è una semplice questione di:

for (string::const_iterator i = s.begin(); i != s.end(); ++i)
{
    cout << bitValues[*i];
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top