I need a function that convert hexadecimal char pointer to string:
ex:
std::string Myfunction(char* hexacode)
{
std::string output;
//
//
return output;
}
std::string Result = Myfunction("\x55\x8B\xEC\x83\xEC\x14\x53\x56\x8B\x75\x0C");
In short I need to convert in string this parameter or similar.
Because in the output the backslash is a option can be a solution replace backslash with slash if is too complicate keep the backslash.
Many thanks !!
If you want the user to input the string, you don't have to care about anything, string escaping only happens for constant strings in the code file.
If you don't want the constant strings in the code file to be escaped, no function conversion needed, just do this:
"\\x55\\x8B\\xEC\\x83\\xEC\\x14\\x53\\x56\\x8B\\x75\\x0C"
Related
Suppose I have a string that contains a necessary numeric character but it is not terminated by '/0', it has garbage characters instead. Actually, the string has garbage characters after the number. So how to deal with the garbage character while storing that numerical character in another string or variable?
So how to deal with the garbage character while storing that numerical character in another string or variable?
Only copy a substring. Example:
std::string example "garbage1garbage";
char numerical = example[7];
We got the numerical character excluding the garbage entirely.
If the text be converted is in a std::string, then you can extract a number from the front as follows:
#include <sstream>
...
std::string input = "128734garbage";
std::istringstream iss{input};
int num;
if (iss >> num)
...use_num...
else
std::cerr << "wasn't able to parse an int from input\n";
Just change int to double, uint64_t, ... - whatever suits your data.
If you have only a pointer to the text and know it's not null-terminated, just getting the text into a std::string is problematic. You could instead use a function that converts text to a number, but stops at the first invalid character. std::stol et al, and the other unsigned and floating point variants linked from the same reference page, are good candidates for that.
From your "another string or variable" - the above addresses storing into a numeric variable. You can then create a new std::string from the number using std::to_string, or a std::ostringstream, if that's what you want to do. This will standardise the output format though, so input like say "1E4" might end up looking like say 1000.0. Alternatively, with the stol-type functions you can use the pointer-to-the-end-of-the-number to work out the length of the numeric part, and use std::string::substr() to extract the leading number as a new std::string object.
You should also be aware that the distinction between number and garbage is not always what you might expect. For example "0XBEFHJQ" might be split by some of the above functions as 0xBEF hex and HJQ garbage.
I'm wanting to save the content of a file to a struct. I've tried to use seekg and read to write to it but it isn't working.
My file is something like:
johnmayer24ericclapton32
I want to store the name, the last name and the age in a struct like that
typedef struct test_struct{
string name;
string last_name;
int age;
} test_struct;
Here is my code
int main(){
test_struct ts;
ifstream data_base;
data_base.open("test_file.txt");
data_base.seekg(0, ios_base::beg);
data_base.read(ts, sizeof(test_struct));
data_base.close();
return 0;
}
It doesn't compile as it don't want me to use ts on the read function. Is there another way - or a way - of doing it?
Serialization/Deserialization of strings is tricky.
As binary data the convention is to output the length of the string first, then the string data.
https://isocpp.org/wiki/faq/serialization#serialize-binary-format
String data is tricky because you have to unambiguously know when the string’s body stops. You can’t unambiguously terminate all strings with a '\0' if some string might contain that character; recall that std::string can store '\0'. The easiest solution is to write the integer length just before the string data. Make sure the integer length is written in “network format” to avoid sizeof and endian problems (see the solutions in earlier bullets).
That way when reading the data back in you know the length of the string to expect and can preallocate the size of the string then just read that much data from the stream.
If your data is a non-binary (text) format it's a little trickier:
https://isocpp.org/wiki/faq/serialization#serialize-text-format
String data is tricky because you have to unambiguously know when the string’s body stops. You can’t unambiguously terminate all strings with a '\n' or '"' or even '\0' if some string might contain those characters. You might want to use C++ source-code escape-sequences, e.g., writing '\' followed by 'n' when you see a newline, etc. After this transformation, you can either make strings go until end-of-line (meaning they are deliminated by '\n') or you can delimit them with '"'.
If you use C++-like escape-sequences for your string data, be sure to always use the same number of hex digits after '\x' and '\u'. I typically use 2 and 4 digits respectively. Reason: if you write a smaller number of hex digits, e.g., if you simply use stream << "\x" << hex << unsigned(theChar), you’ll get errors when the next character in the string happens to be a hex digit. E.g., if the string contains '\xF' followed by 'A', you should write "\x0FA", not "\xFA".
If you don’t use some sort of escape sequence for characters like '\n', be careful that the operating system doesn’t mess up your string data. In particular, if you open a std::fstream without std::ios::binary, some operating systems translate end-of-line characters.
Another approach for string data is to prefix the string’s data with an integer length, e.g., to write "now is the time" as 15:now is the time. Note that this can make it hard for people to read/write the file, since the value just after that might not have a visible separator, but you still might find it useful.
Text-based serialization/deserialization convention varies but one field per line is an accepted practice.
You'll have to develop a specific algorithm, since there is no separator character between the "fields".
static const std::string input_text = "johnmayer24ericclapton32";
static const std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
static const std::string decimal_digit = "0123456789";
std::string::size_type position = 0;
std::string artist_name;
position = input_text.find_first_not_of(alphabet);
if (position != std::string::npos)
{
artist_name = input_text.substr(0, position - 1);
}
else
{
cerr << "Artist name not found.";
return EXIT_FAILURE;
}
Similarly, you can extract out the number, then use std::stoi to convert the numeric string to internal representation number.
Edit 1: Splitting the name
Since there is no separator character between the first and last name, you may want to have a list of possible first names and use that to find out where the first name ends and the surname starts.
I have structure member which takes the value of delimiter from XML.And I want use that structure member as delimiter . But as getline() function take delimiter as char only so my I'm stuck over there.
istream& getline (istream& is, string& str, char delim)
I have done following thing in my code:-
string ABC = struct.delimiter; // Here struct.delimiter is takes value of
d delimiter from XML delimiter = '='
and I want use as
getline(string1,string2, ABC);
Is it possible?
or
Is there another way to do it?
You cannot achieve this with a getLine, the best way I know of to do this would be to get your line, store it in a string and use the find() function to find your delimiters.
string string; // Your line
string ABC = struct.delimiter; // Delimited
getline(input, string); // Get line from stream - Thx Kevin
string subStr = str.substr(0, str.find(ABC)); // this will get you the first segment
From here on you can develop your loop to go over the read data and find all segments.
If ABC is guaranteed to be a single character in length you can use ABC[0] to get the first character out of the string:
getline(string1, string2, ABC[0]);
Or just make ABC a char to make that guarantee even more apparent.
char ABC = struct.delimiter; // (assuming struct.delimiter is also a char,
// if not use srtuct.delimiter[0])
getline(string1, string2, ABC);
I have a stack defined and it accepts only strings.
Now my goal is to read each character, if it is operator like +, I need to push it to the stack.
when i push it, it saves the ACSII calue of + which is 43
how do i achieve to save char in to string?
i tried to use STL
stack<string> s1;
string post;
if (exp[0] == '+')
{
s1.push(to_string(exp[0]));
}
cout<<s1.top();
i need to save +, eventually i am writing code to convert infox to postfix expression. My input will be numbered with operators
The to_string function only has overloads for converting numerical values to strings, which is why you're getting the string "43". You could just change your code to this:
s1.push("+");
in which string's from c-string constructor is used to implicitly convert from the const char* to a string. Alternately, to convert from a single char to a string, the easiest way is to use the fill constructor, in which case you would write this:
s1.push(string(1, exp[0]));
I'm trying to write a function that takes an input string, a regex (made by std.regex.regex from a rawstring) and an error message string, and attempt to match something from the input string using the regex, displaying the error message if there are no matches. I came up with the following signature so far:
string check_for_match (string input, Regex r, string error_message)
However, this doesn't seem to work, as the compiler complains, saying:
struct std.regex.Regex(Char) is used as a type
So what should I use instead?
It'll compile if you change Regex to Regex!char.
The reason is that Regex is a template that can use any character size: char for UTF-8 patterns, wchar for UTF-16, or dchar for UTF-32. The compiler is saying you need to create a type by passing the required Char argument there to use it here.
Since you are working with string, which is made up of chars, Regex!char is the type to use.
string check_for_match (string input, Regex!char r, string error_message) { return null; }