0

I've been researching this problem for a while now, with nothing coming up. So how would I be able to convert a binary string, to something that I can edit like a string.

Something like this (sorry about the pseudo code.)

unsigned char binary = 0100001100001111;

string binaryString = binary.tostring;
//binaryString = 0100001100001111 (as a string)

If this is also possible, I was wondering if it was possible to "remove" certain characters off of a string, and replace them with something else. (Kinda like .remove() in C#.)

EDIT: The binary code is stored in

unsigned char gfx[32 * 64];

And is set in this code:

x = V[(opcode & 0x0F00) >> 8];
y = V[(opcode & 0x00F0) >> 4];
height = opcode & 0x000F;

V[0xF] = 0;
for (int yline = 0; yline < height; yline++)
{
    pixel = memory[I + yline];
    for (int xline = 0; xline < 8; xline++)
    {
        if ((pixel & (0x80 >> xline)) != 0)
        {
            if (gfx[(x + xline + ((y + yline) * 64))] == 1)
            {
                V[0xF] = 1;
            }
            gfx[x + xline + ((y + yline) * 64)] ^= 0x1;
        }
    }
}

Where opcode, V and Pixel are all hexadecimal values.

6
  • 2
    A simple way could be to use a bitset which has a tostring function. Commented Nov 27, 2015 at 5:16
  • 1
    I'm not quite sure if what you're trying to do makes sense. But couldn't you just use the old sprintf function to format your output as a string? Commented Nov 27, 2015 at 5:29
  • 0100001100001111 is an octal constant. Commented Nov 27, 2015 at 5:32
  • 1
    @itsols I hate to ask you, but how exactly would I go about doing that? I came from C#, and I'm still kinda a new to C++. Commented Nov 27, 2015 at 5:33
  • 1
    You are changing your question, and then adding unrelated questions. Commented Nov 27, 2015 at 6:18

2 Answers 2

1

Just for fun:

#include <iostream>

template <typename T>
struct bit_string
{
    char data[sizeof(T) * 8 + 1];

    explicit bit_string(T x)
    {
        constexpr size_t n = sizeof(T) * 8;
        for (size_t i = 0; i != n; ++i)
            data[n - i - 1] = (x & (1 << i)) ? '1' : '0';
        data[n] = '\0';
    }

    operator const char * () const
    {
        return data;
    }
};

int main()
{
    using namespace std;

    cout << bit_string<unsigned short>(1000) << endl;
}

0000001111101000

Sign up to request clarification or add additional context in comments.

Comments

0

What I can see after your edit is that you have an array of chars. So just convert it to a string with the following and then you can use whatever std support you want to process the string. About the rest of your code I am not sure what the goal is.

#include <string>
#include <iostream> 
#include <algorithm> 
int main() {

    // This does what you asked
    char binary[] = {1,0,1};
    std::string strbinary;    
    for (int i=0; i < sizeof(binary); i++) {
        strbinary.append(std::to_string(binary[i]));
    }
    std::cout << strbinary;     

    //This replace
    std::replace( strbinary.begin(), strbinary.end(), '0', 'y');
    std::cout << strbinary;
}

2 Comments

Your code is a bit hard to understand but if I understood correctly you have an array of char which represent a binary sequence. You can easily convert it into string and then do whatever you need with that.
@xbirkettx Thank you so much! That fixed it right up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.