I'm using CryptoPP to encode a string "abc", the problem is the encoded base64 string always has a trailing '0x0a'?
here's my code:
#include <iostream>
#include <string>
using namespace std;
#include "crypto/base64.h"
using namespace CryptoPP;
int main() {
string in("abc");
string encoded;
CryptoPP::StringSource ss(
in,
true,
new CryptoPP::Base64Encoder(
new CryptoPP::StringSink(encoded)
)
);
cout << encoded.length() << endl;// outputs 5, should be 4
cout << encoded;
}
the string "abc" should be encoded to "YWJj", but the result is YWJj\n, (\n == 0x0a), and the length is 5 .
Changing the source string doesn't help, any string will be encrypted with a trailing \n
Why is that?
Thanks
From the Base64Encoder docs, the signature of the constructor is
Base64Encoder (BufferedTransformation *attachment=NULL,
bool insertLineBreaks=true,
int maxLineLength=72)
So you get line breaks by default in your encoded string. To avoid this, just do:
CryptoPP::StringSource ss(
in,
true,
new CryptoPP::Base64Encoder(
new CryptoPP::StringSink(encoded),
false
)
);
Related
I have a string with the value 788597.31 and I am converting this value to double but when I print the variable only 788597 is displayed. I have used std::stod(string) and even stringstream but everytime I get the same previous value. Can anybody help me with this?
I want to store this string value in a double varaible.
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
string s="788597.31";
double d=stod(s);
cout<<d<<" ";
stringstream g;
double a;
g<<s; g>>a;
cout<<a;
return 0;
}
The problem is in how you are printing your result, not in the string parsing. This program:
#include <iostream>
using namespace std;
int main() {
cout << 788597.31 << endl;
return 0;
}
also prints 788597.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << setprecision(10) << 788597.31 << endl;
return 0;
}
prints 788597.31
If you want to see more than the default 6 significant digits your program needs to say so.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str {5, 'c'};
cout << str; // "\005c"
}
Output: c
With gdb, it confirms that str contains "\005c" with
str[0] = '\005'
str[1] = 'c'
Why str[0] is not being printed in output console?
Used c++ version: c++11
ASCII 5 represents a signal intended to trigger a response at the receiving end. It is not visible on the console.
Reference: http://ascii.cl/
For example: try 65 instead of 5, you will see 'A'.
The ASCII number 5 is non printable. ASCII table
ASCII representation of 5 which is 53 is printable.
string str {53, 'c'};
cout << str; // 5c
Since ASCII value of 5 is 53.
So, you can try this way:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str {53, 'c'};
cout << str; // "\005c"
}
I'm trying to combine strings to input text files. My code looks like this:
`#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
int main(){
int year;
string line;
string fileName;
for (int i=1880; i<2012; i++){
stringstream ss;
ss << year;
fileName = string("yob") + string(year) + string(".txt");
ifstream ifile(fileName.c_str());
getline(ifile,line);
cout << line << endl;
ifile.close();
}
}`
The text files look like "yob1880.txt" <-- that's the first text file and it goes all the way to "yob2011.txt". I want to input the text files one by one, but combining these three string types doesn't work it gives me an error saying invalid conversion from int to const char*.
Any thoughts on the problem? Thanks!
You should get it from the stringstream. You're almost there but this is what you should do instead:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
int main(){
int year;
string line;
string fileName;
for (int i=1880; i<2012; i++){
year = i; //I'm assuming this is what you meant to use "year" for
stringstream ss;
ss << year; //add int to stringstream
string yearString = ss.str(); //get string from stringstream
fileName = string("yob") + yearString + string(".txt");
ifstream ifile(fileName.c_str());
getline(ifile,line);
cout << line << endl;
ifile.close();
}
}
It's only every once in awhile that I have to write in c++. My problem is how do you find a character string within a string? Then save that line, and then look for a set of numbers in that line. For example I have a text file that looks like this.
Address Length Name
87623498 2 dog
12345678 4 cat
98737289 1 bird
I want to search for "cat" and then store the numbers associated with it (12345678) and (4) to different variable names. Here is some code I have written, but it's not close to being correct. I have an .exe that calls this DLL. Any help is appreciated!
`
#include
#include
#include
#include
#include
char* File_Path; //Path is sent by another program
char* Name; //value is being sent by another program
unsigned long Address;
int Length;
int found = 0;
{
using namespace std;
std::string Line;
std::ifstream infile;
infile.open (File_Path);
if (infile.is_open())
{
std::ofstream outfile;
outfile.open ("C:\\debug\\test.txt")
while (infile.good() && found == 0)
{
std::getline(infile,Line);
//if (Name is within Line){
/*I want to say IF char* Name = a part of the string line
ie. Name = cat and the string Line is (12345678 4 cat)
Then store the number (12345678 = Address), and
(4 = Length) I hope that makes sense :/
*/
outfile << Line; //output the stored line for debug
outfile << address; //output the stored address for debug
outfile << length; //output the stored length for debug
found = 1;
}
outfile.close();
infile.close();
}
return 0;
}
`
Please help! Thank you!
I programmed a little Application in C++. There is a ListBox in the UI. And I want to use the selected Item of ListBox for an Algorithm where I can use only wstrings.
All in all I have two questions:
-how can I convert my
String^ curItem = listBox2->SelectedItem->ToString();
to a wstring test?
-What means the ^ in the code?
Thanks a lot!
It should be as simple as:
std::wstring result = msclr::interop::marshal_as<std::wstring>(curItem);
You'll also need header files to make that work:
#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>
What this marshal_as specialization looks like inside, for the curious:
#include <vcclr.h>
pin_ptr<WCHAR> content = PtrToStringChars(curItem);
std::wstring result(content, curItem->Length);
This works because System::String is stored as wide characters internally. If you wanted a std::string, you'd have to perform Unicode conversion with e.g. WideCharToMultiByte. Convenient that marshal_as handles all the details for you.
I flagged this as a duplicate, but here's the answer on how to get from System.String^ to a std::string.
String^ test = L"I am a .Net string of type System::String";
IntPtr ptrToNativeString = Marshal::StringToHGlobalAnsi(test);
char* nativeString = static_cast<char*>(ptrToNativeString.ToPointer());
The trick is make sure you use Interop and marshalling, because you have to cross the boundary from managed code to non-managed code.
My version is:
Platform::String^ str = L"my text";
std::wstring wstring = str->Data();
With Visual Studio 2015, just do this:
String^ s = "Bonjour!";
C++/CLI
#include <vcclr.h>
pin_ptr<const wchar_t> ptr = PtrToStringChars(s);
C++/CX
const wchart_t* ptr = s->Data();
According to microsoft:
Ref How to: Convert System::String to Standard String
You can convert a String to std::string or std::wstring, without using PtrToStringChars in Vcclr.h.
// convert_system_string.cpp
// compile with: /clr
#include <string>
#include <iostream>
using namespace std;
using namespace System;
void MarshalString ( String ^ s, string& os ) {
using namespace Runtime::InteropServices;
const char* chars =
(const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}
void MarshalString ( String ^ s, wstring& os ) {
using namespace Runtime::InteropServices;
const wchar_t* chars =
(const wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}
int main() {
string a = "test";
wstring b = L"test2";
String ^ c = gcnew String("abcd");
cout << a << endl;
MarshalString(c, a);
c = "efgh";
MarshalString(c, b);
cout << a << endl;
wcout << b << endl;
}
output:
test
abcd
efgh