1

In c++ I have an array of signed long long (63bit numbers), array of variable lenght.

std::array<long long, n> encodedString

This array is in fact holding an UTF-8 encoded string. Meaning that if you concatenate the binaries of each element of the array, the result will be an UTF-8 encoded text.

For example the array :

(621878499550 , 2339461068677718049) 

If you translate those signed long long in 63 bit binary it gives :

621878499550 = 000000000000000000000001001000011001010110110001101100011011110

2339461068677718049 = 010000001110111011011110111001001101100011001000010000000100001

If you concatenate those binaries into : 000000000000000000000001001000011001010110110001101100011011110010000001110111011011110111001001101100011001000010000000100001

This is the UTF8 for "Hello world !"

So the question is what is the easiest way to get a string with "Hello world !" starting with the array (621878499550 , 2339461068677718049)

Best solution I currently have is to write the array to a file in binary mode (fwrite) then read the file in text mode to a string.

PaddleStroke
  • 63
  • 1
  • 6
  • Note that `signed long long` is still 64 bits (at least). – Some programmer dude Jan 28 '19 at 08:39
  • Write the array to a string stream instead of a file. Then you have the same operation but entirely in memory instead of using an external file. – john Jan 28 '19 at 08:40
  • As for your problem, you need to get the data into an array of bytes (i.e. an array of `char` basically). Then skip the leading zero bytes. Then you can copy the remaining data into a `std::string`. Using an intermediate `char` buffer is really the only way to not be bothered by [the strict aliasing rule](https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule). – Some programmer dude Jan 28 '19 at 08:41
  • Yes you are right it's still 64 bits, so I need to get the sign bit off. – PaddleStroke Jan 28 '19 at 08:41
  • Your sample array has the same value for both elements. That translates to `' world ! world !'`. – Mark Tolonen Jan 28 '19 at 08:53
  • My mistake, just corrected it – PaddleStroke Jan 28 '19 at 11:06

2 Answers2

0

Try this

// 48 & 56 were to avoid the extra padding made when you use 64 bitset but i think thats what you are looking for 
std::string binary = std::bitset<48>(114784820031264).to_string();
std::string binary2 = std::bitset<56>(2339461068677718049).to_string();
binary += binary2;
std::stringstream sstream(binary);
std::string output;
while (sstream.good())
{
    std::bitset<8> bits;
    sstream >> bits;
    output +=char(bits.to_ulong());

}

std::cout << output;
Spinkoo
  • 2,080
  • 1
  • 7
  • 23
  • Thanks for reply. However what I need is to get as output the string "Hello world !", this output the binary sequence as a string. Any ideas? – PaddleStroke Jan 28 '19 at 13:25
0

Use bitset to convert a long long to binary and string stream to stream them

#include <sstream>
#include <iostream>
#include <bitset>
#include <array>

int main() 
{
    std::array<long long, 2> array = { 621878499550 , 2339461068677718049ll };
    std::stringstream ss;

    for (auto& n : array)
    {
        ss << std::bitset<64>(n);
    }

    std::cout << ss.str() << std::endl;
}

which outputs 0000000000000000000000010010000110010101101100011011000110111100010000001110111011011110111001001101100011001000010000000100001

PaddleStroke
  • 63
  • 1
  • 6
  • Thanks for reply. However what I need is to get as output the string "Hello world !", not the binary as a string. Any ideas? – PaddleStroke Jan 28 '19 at 13:25