简体   繁体   English

将十六进制字符输入到stringstream

[英]feeding hex characters to stringstream

I'm trying to set bits in a binary string. 我正在尝试在二进制字符串中设置位。 I initially have an empty string which needs to set the give bit(i) in the string(s). 我最初有一个空字符串,需要在字符串中设置给定位(i)。

for the given example, the output should be 0x3001 as: 对于给定的示例,输出应为0x3001如下所示:

pos: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
bit: 0 0 1 1 0 0 0 0 0 0  0  0  0  0  0  1
     ^                                   ^
     MSB                               LSB

Which, in hex is 3001 . 以十六进制表示的是3001

#include<iostream>
#include<string>
#include<sstream>
#include<iomanip>

using namespace std;
void generate_string(string& s,int i){
    int sl = s.length();
    int x = i/8;
    std::stringstream m(s, ios_base::app);
    if(sl<x){
        for(int j = x-sl;j>=0;j--){
            m<<'\x00';
        }
    }
    s = m.str();
    s[x] |= 1 << i%8;
}
int main(){
    string s = "";
    generate_string(s,15);
    generate_string(s,2);
    generate_string(s,3);
    for(int i=0;i<s.length();i++)
        cout<<hex<<(int)s[i];
    return 0;
}

But this program is not showing any output. 但是此程序未显示任何输出。

It's actually much more simpler than you think. 实际上,它比您想象的要简单得多。 The only complicated part is to calculate the bit number to be set in the byte. 唯一复杂的部分是计算要在字节中设置的位数。

Oh, and why use a string for this? 哦,为什么要使用字符串呢? Why not a vector ? 为什么不是向量

Here's my solution, using std::vector instead: 这是我的解决方案,改用std::vector

void set_bit(std::vector<uint8_t>& bits, unsigned bit)
{
    static unsigned const bit_count = 8;   // Should really use std::numeric_limits<uint8_t>::digits

    unsigned index = bit / bit_count;

    while (index + 1 > bits.size())
        bits.push_back(0);

    // Since the bit-numbers are reversed from what's "common",
    // we need a little more complex calculation here.
    // bit % bit_count to get the "normal" bit number
    // bit_count - bit % bit_count to reverse the bit numbering
    // Then -1 to get a number between 0 and 7
    bits[index] |= 1 << (bit_count - bit % bit_count - 1);
}

You can use a similar solution using std::string too, but I don't see why. 也可以使用std::string使用类似的解决方案,但是我不明白为什么。

Maybe like this? 也许是这样吗?

#include<iostream>
#include<string>

using namespace std;
void set_bit(string& s,int i){
    auto bits = ((i + 7) / 8) * 8;
    if (bits > s.length())
    {
        auto diff = bits - s.length();
        s += std::string(diff, '0');
    }
    s[i] = '1';
}

int main(){
    string s;
    set_bit(s, 2);
    set_bit(s, 3);
    set_bit(s, 15);
    cout << s << endl;
    return 0;
}

expected output: 预期输出:

0011000000000001

update: attempt 2 :-) 更新:尝试2 :-)

#include<iostream>
#include<iomanip>
#include<string>

using namespace std;
void set_bit(string& s,int i){
    auto bytes = (i + 7) / 8;
    if (bytes > s.length())
    {
        auto diff = bytes - s.length();
        s += std::string(diff, 0);
    }
    s[i / 8] |= char(1 << (7-(i%8)));
}

int main(){
    string s;
    set_bit(s, 2);
    set_bit(s, 3);
    set_bit(s, 15);

    std::cout << "as hex: ";
    for (auto c : s) {
        cout << hex << setfill('0') << setw(2) << (int(c) & 0xff);
    }
    cout << endl;

    std::cout << "as binary: ";
    auto sep = "";
    for (auto c : s) {
        unsigned char bits = c;
        for (unsigned char mask = 0x80 ; mask ; mask >>= 1)
        {
            cout << sep << ((bits & mask) ? '1' : '0');
            sep = " ";
        }
    }
    cout << endl;



    return 0;
}

expected output: 预期输出:

as hex: 3001
as binary: 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1

I don't quite understand what the output should be in Your problem, because You are mixing most/least signifanct bits with nibbles order in sample input/output, but I You would like to print number in hex as a string You can do sth like this: 我不太了解您的问题应该是什么输出,因为您在示例输入/输出中混合了大多数/最低有效位和半字节顺序,但是我想将十六进制数字打印为字符串。像这样:

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>

void feed(std::string& s, int x){
unsigned int mask = 15;
int nibblesInWord = sizeof(void*)*16;
std::stringstream ss;
while(nibblesInWord--){
 std::cout << int(x & mask) <<std::endl;
 ss << int(x & mask);
 x >>= 4;
}
s = ss.str();
std::reverse(s.begin(), s.end());
}


int main(){
std::string s;
 feed(s, 99);
 std::cout << s <<std::endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM