Domanda

I am trying to extract 4-bits from a 16-bit binary string, i.e nibbles out of a word Can anyone tell me what is wrong with this program?

#include <sstream>
#include <iomanip>
#include <math.h>
#include<iostream>

using namespace std;

int main()
{
    std::string aBinaryIPAddress = "1100110011001100";

    std::string digit0 = aBinaryIPAddress & 0x0f;
    cout << "digit0 : " << digit0 << endl;

    std::string digit1 = (aBinaryIPAddress >>  4) 0x0f;
    cout << "digit1 : " << digit1 << endl;

    std::string digit2 = (aBinaryIPAddress >>  8) 0x0f;
    cout << "digit2 : " << digit2 << endl;

    std::string digit3 = (aBinaryIPAddress >> 12) 0x0f;
    cout << "digit3 : " << digit3 << endl;

    return 0;
}

I am getting the following error:

 changes.cpp: In function `int main()':
 changes.cpp:117: error: invalid operands of types `char*' and `int' to binary `
 operator>>'
 changes.cpp:117: error: parse error before numeric constant
È stato utile?

Soluzione

If you are manipulating a string, you should be using substr, not "shift and mask" technique: & and >> operators are undefined for strings and ints.

Here is how to do it with substr:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string aBinaryIPAddress = "0101100111101101";
    size_t len = aBinaryIPAddress.size();
    for (int i = 0 ; i != 4 ; i++) {
        cout << "Digit " << i << ": " << aBinaryIPAddress.substr(len-4*(i+1), 4) << endl;
    }
    return 0;
}

This prints

Digit 0: 1101
Digit 1: 1110
Digit 2: 1001
Digit 3: 0101

Demo on ideone.

If you need four individual variables, "unroll" the loop, like this:

string d0 = aBinaryIPAddress.substr(len-4, 4);
string d1 = aBinaryIPAddress.substr(len-8, 4);
string d2 = aBinaryIPAddress.substr(len-12, 4);
string d3 = aBinaryIPAddress.substr(len-16, 4);

Altri suggerimenti

You can not convert from an integer to string the way you do. You can use stringstream:

#include <iomanip>
......
std::string convert_int_to_hex_stiring(int val) {
  std::stringstream ss;
  ss << std::hex << val;
  return val;
}

Before you use the function above you will have to read integers from parts of the input string. Problem is that the bitwise operations are not defined on a string.

the type of aBinaryIPAddress should be a number rather than a string

something like

unsigned int aBinaryIPAddress = 0b1100110011001100;

should work

You have a std::string object that holds 16 characters, each with the value '0' or '1'. If you want to look at "nibbles" taken from there, just pull out the 4-character groups that you need:

std::string digit0 = aBinaryIPAddress.substr(12,4);
std::cout << digit0 << '\n';

That's just text manipulation; if you want to get values, it's straightforward (and a useful exercise) to convert the characters in digit0 into a numeric value..

It is obvious that the problem is in statements like this

std::string digit0 = aBinaryIPAddress & 0x0f;

Do you understand that std::string is not a number?!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top