2
votes

I'm trying the code mentioned here:

std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
}

And pass: base64_encode("mystring", 8).

It shows a type conversion error:

error C2664: 'base64_encode' : cannot convert parameter 1 from 'const char [9]' to 'const unsigned char *

2
I think your main issue is unsigned char const* try just having it be a char*Zack Newsham
Your string literal is an array of char, not an array of unsigned char. You'll either need to cast it, or use an array of unsigned char as your input.pburka
I don't understand why it was a "const" in the first place. But it seems to work now.user2723500

2 Answers

1
votes

I just came across this post and wanted to clarify a bit for anyone else trying to base64 encode a string. The code referred to by the original poster comes from the web page: http://www.adp-gmbh.ch/cpp/common/base64.html. If you go to the author's page and look under the "The test file" section you will see exactly how the author recommends the code to be used.

const std::string s = "test string" ;
std::string encoded = base64_encode(reinterpret_cast<const unsigned char*>(s.c_str()), s.length());

I tried it in my program and it seems to work perfectly.

0
votes

I don't think you can have string literals using unsigned chars. The easiest way to create a sequence of unsigned chars from a string literal is probably

char const* literal = "hello";
std::vector<unsigned char> ustr(literal, literal + std::strlen(literal));
unsigned char const* ustrptr = ustr.data();

Obviously, the logic could be encapsulated into a suitable function calling base64_encode().

The alternative is to just reinterpret_cast<unsigned char const*>("hello") but I'm personally not a big fan of this approach.