-1

I am using the base64 function from here to try and run a simple program. I want to convert binary to base64 using this code:

int x = 1;
std::string data = base64_encode(reinterpret_cast<unsigned char *>((void *)&x), 4);
std::string out = base64_decode(data);
int y = reinterpret_cast<int>(out.data());

The encode function is called, and generates this string "AQAAAA==". From my understanding, this would should convert to 01 00 00 0 if decoded and converted to bytes (I actually don't understand why it's 7 bytes). When the decode function is called, I expect 01 00 00 00, which would then be reinterpret_castable back into the integer 1, but rather I get "\001\000\000", which is not what I expect. I tried increasing the second parameter of the encode function, funnily, it gives me the proper answer of "\001\000\000\000" after decode.

James Meas
  • 327
  • 4
  • 16
  • An [mcve] would allow us to reproduce your problem... – DevSolar Apr 11 '18 at 08:27
  • I figured my code example meets the criteria for that. Let me know if I can change it in any way. – James Meas Apr 11 '18 at 08:29
  • Can I copy, paste, compile, run, see your results? No. Anyway, I am *very much* doubting your `reinterpret_cast<>`s are a good idea... – DevSolar Apr 11 '18 at 08:30
  • Actually, after pasting the linked implementation and your code into a whole, I get this: `error: cast from 'const char *' to 'int' loses precision`. So your code doesn't compile... – DevSolar Apr 11 '18 at 08:31
  • Ok, right. However, I'm trying to balance minimal and complete. – James Meas Apr 11 '18 at 08:32
  • It compiles on my system. – James Meas Apr 11 '18 at 08:33
  • The point in "complete" is that, to check if any suggestion *actually* works, *every potential answer* includes cobbling the parts together to something that compiles (which your code doesn't, see above). That's why we're asking for questions that *don't* require that kind of footwork for an answer. -- The combination of `reinterpret_cast<>` and "works on *my* system" should ring all kinds of bells. ;-) – DevSolar Apr 11 '18 at 08:34
  • I'd like to mention again that the code above, copied directly, compiles on my system. I'll make an effort to include a more complete code that makes it easier for one to test next time. – James Meas Apr 11 '18 at 08:39
  • The underlying idea is that attempting to achieve a *minimal* example is important debug work, which quite frequently reveals the problem before you even ask. For example, you'd rather avoid pasting the full base64 implementation. Can you "mock" it, and still get the kind of surprising result? This makes you look at your intermediate results, and you can either pinpoint a specific behaviour of the base64 implementation, or an issue with your casts for example. You take variables out of the equation, making the example simpler, until either the problem is obvious or the question more focussed. – DevSolar Apr 11 '18 at 10:44

1 Answers1

2

Assuming you are using this code for base64, I suspect the following will work:

int x = 1;
std::string data = base64_encode(reinterpret_cast<unsigned char *>(&x), 4);
std::string out = base64_decode(data);
int y= *(reinterpret_cast<int*>(out.c_str()));

On whatever side is doing the decode logic, it should probably validate that out.size() == sizeof(int) before doing this conversion.

selbie
  • 100,020
  • 15
  • 103
  • 173
  • That works, just need a const_cast in there. Thank you. – James Meas Apr 11 '18 at 08:37
  • I thought y = reinterpret_cast(out.c_str()) would work the same as memcpy(&y, out.c_str(), 4), but it turns out memcpy gives the correct result while the first gives a garbled result. – James Meas Apr 11 '18 at 18:41