How can i fill a char array with some string , en int - c++

I have a question about a char array:
I have a form '"1"+lapcounter+":"+seconds' that must come in a char array.
How can i fill this array in this form?
Thanks

If you mean you have some numeric variables which you want to format into a string, use a string-stream for that:
std::stringstream ss;
ss << "1" << lapcounter << ":" << seconds";
Now you can extract a string from that:
std::string s = ss.str();
and if you really want a character array for some reason (which I'm sure you don't)
char const * cs = s.c_str();

Use sprintf, or snprintf. This function works similar to printf but instead of standard output, the output will go to char array you specified. For example:
char buffer[32];
snprintf(buffer, sizeof(buffer), "1%d:%d", lapcounter, seconds);

to_string is used like this:
#include <iostream>
#include <string>
int main()
{
int lapcounter = 23;
std::string str("1");
str.append(std::to_string(lapcounter ));
str.append(":seconds");
std::cout << str << std::endl;
}
prints
123:seconds
if you really need a char array you get that from ss.c_str()

Related

c++ cstring, Is there any way to get single char from cstring?

char cstri[] = "hello world";
From here, is there any way to get a single char, such as the first e, position at 1, from this cstring?
I tried a few times, and every time it returns the entire string starting from the passed index. So, if I want 'e', position at 1, it returns ello world instead of just e.
I also tried to copy a single char from the string using strncpy() and memcpy(), but it copies the string from index 0 to null, or just the specified amount.
strncpy(b, cstri , 1);
I know a cstring is read-only, but is there no way to get a single char from a cstring?
I want to use printf(), so I can't use char b = cstri[1]
Your question already hints on "C++" and given the additional fact that there is no difference in the memory representation of a C++ std::string and a normal c-string, the answer is simple: just learn how std::string works. Btw. std::string::c_str() then return your nice c-string.
#include <string>
#include <iostream>
int main() {
std::string hw = "hello world";
std::cout << hw << std::endl; // output: "hello world"
std::cout << hw.c_str() << std::endl; // output: "hello world"
std::cout << hw[1] << std::endl; // output: 'e'
return 0;
}
But there are a ton of more features of std::string and also look at what #include <iomanip> can do.

Convert string in '\\x00\\x00\\x00' format to unsigned char array

Say I have a string as so:
std::string sc = "\\xfc\\xe8\\x82";
how could I convert the sc string into the equivalent of
unsigned char buf[] = "\xfc\xe8\x82";
I'm trying to convert a string containing shellcode into a unsigned char array.
I have tried the following:
char buf[5120];
strncpy(buf, sc.c_str(), sizeof(buf));
buf[sizeof(buf) - 1] = 0;
This seems to store strings into the char array I need the char array to store/represent bytes.
When I print:
//example 1
unsigned char buf[] = "\xfc\xe8\x82";
printf("%s", buf);
The console outputs:
ⁿΦé
When I print:
//example 2
char buf[5120];
strncpy(buf, sc.c_str(), sizeof(buf));
buf[sizeof(buf) - 1] = 0;
The Console outputs:
\xfc\xe8\x82
How do I convert the sc string into a unsigned char array so that when sc is printed sc will produce the same output of example 1.
The literal "\\xfc\\xe8\\x82" as a string uses "\" as an escape character. "\\" will be reduced to "\". As you would expect. So, if you print your given std::string, then the result will be:
\xfc\xe8\x82.
So, what you want to do now is: Create a char array containing those hex values, given in the original std::string.
Please note: Your statement char s[] = "\xfc\xe8\x82"; will create a C-Style array of char, with the size 4 and containing:
s[0]=fc, s[1]=e8, s[2]=82, s[3]=0
In the example below I show 2 proposals for conversion.
1. Straight forward conversion
2. Using C++ standard algorithms
#include <string>
#include <iostream>
#include <iomanip>
#include <regex>
#include <vector>
#include <iterator>
#include <algorithm>
// Hex digit String
std::regex hexValue{R"(\\[xX]([0-9a-fA-F][0-9a-fA-F]))"};
int main ()
{
// Source string
std::string s1 = "\\xfc\\xe8\\x82";
std::cout << "s 1: " << s1 << "\n";
// Proposal 1 ------------------------------------------------------
// Target array
unsigned char s2[3];
// Convert bytes from strings
for (int i=0; i<s1.size()/4; ++i ) {
// Do conversion. Isolate substring, the convert
s2[i] = std::strtoul(s1.substr(i*4+2,2).c_str(), nullptr,16);
// Result is now in s2
// Output value as tring and decimal value
std::cout << s1.substr(i*4+2,2) << " -> " << std::hex << static_cast <unsigned short>(s2[i])
<< " -> " << std::dec << static_cast <unsigned short>(s2[i]) << "\n";
}
// Proposal 2 ------------------------------------------------------
// Get the tokens
std::vector<std::string> vstr(std::sregex_token_iterator(s1.begin(),s1.end(),hexValue, 1), {});
// Convert to unsigned int
std::vector<unsigned int> vals{};
std::transform(vstr.begin(), vstr.end(), std::back_inserter(vals),
[](std::string &s){ return static_cast<unsigned>(std::strtoul(s.c_str(), nullptr,16)); } );
// Print output on std::cout
std::copy(vals.begin(), vals.end(), std::ostream_iterator<unsigned>(std::cout,"\n"));
return 0;
}
The second solution will eat any number of hex numbers given in a string

Splitting a string into an array of characters using a variable for the string

I'm trying to split a string into an array of individual characters. However, I would like the string to be input by the user, for which I need to define the string using a variable.
My question is, why does this work:
#include <iostream>
using namespace std;
int main() {
char arr [] = {"Giraffe"};
cout << arr[0];
return 0;
}
But this doesn't?
#include <iostream>
using namespace std;
int main() {
string word;
word = "Giraffe";
char arr [] = {word};
cout << arr[0];
return 0;
}
Thanks
Your example doesn't work because you're trying to put a std::string into an array of char. The compiler will complain here because std::string has no type conversion to char.
Since you're just trying to print the first character of the string, just use the array accessor overload of std::string, std::string::operator[] instead:
std::string word;
word = "Giraffe";
std::cout << word[0] << std::endl;
In your second example, the type of word is a std::string and there are no default type conversions from std::string to the type char.
On the other hand, the first example works because it can be interpreted as an array of char (but actually its just c-style const char *).
If, for some reason, you would want to convert std::string into the c-style char array, you might want to try something like this...
#include <iostream>
#include <string>
#include <cstring>
int main(void)
{
std::string word;
word = "Giraffe";
char* arr = new char[word.length() + 1]; // accounting for the null-terminating character
strcpy(arr, word.data());
std::cout << arr[0] << std::endl;
delete[] arr; // deallocating our heap memory
return 0;
}

string (from substr) conversion to unsigned int

I have a string which actually contains a number and a string, separated by ,, for instance "12,fooBar".
I would like to put it into separated variables, i.e. the number into unsigned int myNum and the string into std::string myStr.
I have the following snipped of code:
size_t pos1=value.find(',');
std::cout << value.substr(0, pos1) << " and "
<< (value.substr(0, pos1)).c_str() << std::endl;
This yields 12 and 1. Anything I missed here? What happend to the 2 in the second part?
Note: I isolated the problem to this snipped of code. I need c_str() to pass it to atoi to get the unsigend int. Here I don't want to print the second part.
Update: I actually get the string from levelDB Get. If I put a test string like I put here, it works.
The posted code produces the same substring: value.substr(0, pos1). Note that std::string::substr() does not modify the object, but returns a new std::string.
Example:
#include <iostream>
#include <string>
int main ()
{
std::string value ="12,fooBar";
unsigned int myNum;
std::string myStr;
const size_t pos1 = value.find(',');
if (std::string::npos != pos1)
{
myNum = atoi(value.substr(0, pos1).c_str());
myStr = value.substr(pos1 + 1);
}
std::cout << myNum << " and "
<< myStr << std::endl;
return 0;
}
Output:
12 and fooBar
EDIT:
If the unsigned int is the only piece required then the following will work:
unsigned int myNum = atoi(value.c_str());
as atoi() will stop at the first non-digit character (excluding optional leading - or +), in this case the ,.
The cleanest C++ style solution to this problem is to use a stringstream.
#include <sstream>
// ...
std::string value = "12,fooBar";
unsigned int myNum;
std::string myStr;
std::stringstream myStream(value);
myStream >> myNum;
myStream.ignore();
myStream >> myStr;
Your second substr should be value.substr(pos1+1,value.length())
One more option is using std::from_chars function from the 17th standard (< charconv > header):
int x;
from_chars(&s[i], &s.back(), x); // starting from character at index i parse
// the nearest interger till the second char pointer
There are different overloads for different types of value x (double etc.).

converting integer to string C++

I am trying to convert an integer to char array and I came across this piece of code
int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();
But when I try to print the value of s it still prints 5. I don't know if its supposed to do that or am I doing something wrong? Besides I would prefer if I could convert the same int to char array. But I would appreciate any help in the matter.
Thanks!
Code taken from: Alternative to itoa() for converting integer to string C++?
Yes, it's supposed to do that. You'd (primarily) notice the difference from just printing a number out directly if you do some other string-type manipulation on the result (e.g., concatenating it with other strings, searching for characters in the string).
Just for example:
std::cout << i+i; // should print "10"
std::cout << s+s; // should print "55"
Besides I would prefer if I could convert the same int to char array.
char *charPtr = new char[ s.length() + 1 ] ; // s is the string in the snippet posted
strcpy( charPtr, s.c_str() ) ;
// .......
delete[] charPtr ; // Should do this, else memory leak.
If you would like to stop worrying about issues like that you might be interested in boost/lexical_cast.hpp.
#include <boost/lexical_cast.hpp>
#include <string>
#include <iostream>
int main() {
const int i=5;
const char* s = boost::lexical_cast<std::string>(i).c_str();
std::cout << s << std::endl;
}