3

I am converting some source code from one scripting language (PAWN) to a programming language (C++) on Windows.

The source code has millions of binary literals all over the place in the form of:

data[] = 
{
    0b11111111111011111110110111111110, 0b00000000001111111111111111111111,
    0b00000000000000000000000000000000, 0b00000000000000000000000000000000,
    0b00000000000000000000000000000000, 0b00000000000000000000000000000000,
    0b00000000000000000000000000000000, 0b00000000000000000000000000000000,
    ///some million lines later...
    0b00000000000000000000000000000000, 0b11111111111111111111111110000000,
    0b11100001001111111111111111111111, 0b11110111111111111111111111111111,
    0b11111111111111111111111111111111, 0b11111111111111111111111111111111,

To my unfortunate luck Visual Studio 2013 doesn't support the user defined literals standard.

Is there any wya to achieve this somehow? 010101_b or something with C++, maybe with a little addition of boost?

Gizmo
  • 1,990
  • 1
  • 24
  • 50
  • possible duplicate of [Binary literals?](http://stackoverflow.com/questions/537303/binary-literals) – 2501 Nov 22 '14 at 16:36
  • Search is your friend. – 2501 Nov 22 '14 at 16:36
  • 1
    Why don't you write down a script using your favorite scripting language (mine's Python for example), that will take this file as input and generate a C++ fie as output? – barak manos Nov 22 '14 at 16:43
  • not a bad idea indeed, some extra time but okay, hoever still doesn't answet the question and doing this over and over for millions of lines, I'd preffer a Replace-All-able solution ;) and I got one of the answers from http://stackoverflow.com/questions/537303/binary-literals , answer: http://stackoverflow.com/a/537346/2548287 I just did: `#include const unsigned long GETBOOL(std::string str) { return std::bitset<32>(str).to_ulong(); } GETBOOL("11111111111011111110110111111110"),` – Gizmo Nov 22 '14 at 16:56

2 Answers2

1

I strongly advise you to transform the source code with a script.

Anyway, if you're interested in Boost.PP:

#define FROM_BINARY(s, data, elem) sum(#elem+2)

constexpr auto sum(char const* str, std::uintmax_t val = 0) -> decltype(val)
{
    return !*str? val : sum(str+1, (val << 1) + *str - '0');
}

unsigned data[]
{
    BOOST_PP_TUPLE_REM_CTOR(BOOST_PP_SEQ_TO_TUPLE(
        BOOST_PP_SEQ_TRANSFORM(FROM_BINARY, , 
            BOOST_PP_VARIADIC_TO_SEQ(
    0b11111111111011111110110111111110, 0b00000000001111111111111111111111,
    0b00000000000000000000000000000000, 0b00000000000000000000000000000000,
    0b00000000000000000000000000000000, 0b00000000000000000000000000000000,
    0b00000000000000000000000000000000, 0b00000000000000000000000000000000,
    ///some million lines later...
    0b00000000000000000000000000000000, 0b11111111111111111111111110000000,
    0b11100001001111111111111111111111, 0b11110111111111111111111111111111,
    0b11111111111111111111111111111111, 0b11111111111111111111111111111111))))
};

Note that this could horribly slow down compile time. So, again, try to transform the source dode once instead.

Columbo
  • 60,038
  • 8
  • 155
  • 203
0

I suggest writing a simple, small, console program that converts the binary literals into hexadecimal literals.

Here's the process I would use:

  1. Copy the file.
  2. Using a text editor, remove everything before the first literal.
  3. Remove everything after the last literal.
  4. Save.
  5. Replace the ", " with a newline.
  6. Save.

You now have a text file with the binary literals, one per line.

Write a program to read in the file, convert to hexadecimal literals and output.

Then edit this file by pasting in all the missing stuff.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154