Converting a string to an int using str.at(x) - c++

I'm constructing a line command based chess engine. When a command is given to the engine (as a string), I have a couple of regex expressions run to determine if the command is valid. If it's valid, then the command is passed on to be parsed.
When a command such as a6a4 is given to the engine in string input;, is it valid to use a command like:
istringstream (input.at(1)) >> originalx; or:
istringstream buffer(input.at(1)); buffer >> originalx;
to convert just the part of the string with the integer into an actual int? I have been trying these methods of conversion, all using str.at(x) rather than just str and none of them seem to be working.
I know this may seem like a duplicate question because everyone wants to know how to convert an entire string to an int, but what about just a specific part of a string? I did search Google for answers as well but nothing I found was helpful.

I know this may seem like a duplicate question because everyone wants to know how to convert an entire string to an int, but what about just a specific part of a string?
A specific part of a string is still a string. You can just do str.substr(1, 1) and convert that whole sub-string to an int.

Related

How can I replicate compile time hex string interpretation at run time!? c++

In my code the following line gives me data that performs the task its meant for:
const char *key = "\xf1`\xf8\a\\\x9cT\x82z\x18\x5\xb9\xbc\x80\xca\x15";
The problem is that it gets converted at compile time according to rules that I don't fully understand. How does "\x" work in a String?
What I'd like to do is to get the same result but from a string exactly like that fed in at run time. I have tried a lot of things and looked for answers but none that match closely enough for me to be able to apply.
I understand that \x denotes a hex number. But I don't know in which form that gets 'baked out' by the compiler (gcc).
What does that ` translate into?
Does the "\a" do something similar to "\x"?
This is indeed provided by the compiler, but this part is not member of the standard library. That means that you are left with 3 ways:
dynamically write a C++ source file containing the string, and writing it on its standard output. Compile it and (providing popen is available) execute it from your main program and read its input. Pretty ugly isn't it...
use the source of an existing compiler, or directly its internal libraries. Clang is probably a good starting point because it has been designed to be modular. But it could require a good amount of work to find where that damned specific point is coded and how to use that...
just mimic what the compiler does, and write your own parser by hand. It is not that hard, and will learn you why tests are useful...
If it was not clear until here, I strongly urge you to use the third way ;-)
If you want to translate "escape" codes in strings that you get as input at run-time then you need to do it yourself, explicitly.
One way is to read the input into one string. Then copy the characters from that source string into a new destination string, one by one. If you see a backslash then you discard it, fetch the next character, and if it's an x you can use e.g. std::stoi to convert the next few characters into its corresponding integer value, and append that number to the destination string (either adding it with std::to_string, or using output string streams and the normal "output" operator <<).

Arduino - Separate the integer of the String in Serial.ReadString()

I'm starting with Arduino programming, and I'm developing a program that will receive an string and a integer, together, for example: "pisca 10". How can I separate it in a String(pisca) and a integer(10) variable? The integer will be applied in a For() loop and the String will be tested with another. How can I do it?
You will have to do it in two steps:
Read the complete string (which is "picsa 10" in your example) and store it into a variable as a String.
Extract the number part from the stored string.
For 2, you can implement this yourself as an exercise, but you can also use sscanf function which purpose is to parse a string according to a format. Please see the example in the linked page.

Split and Assign char

I am new to C++ and Arduino.
I have 3 variable of type char
char receivePayload[31];
char devID;
char switchState;
the data in receivePayload woudl be some like "01:01" or "01:00". Here the first part before colon is device ID and second part is switchState.
Can you please help me and explain how I can split the 2. I did read about strtok but was unable to understand.
The desired output would be like
devID would have 01 stored and
switchState would have 01 or 00 stored.
Also if I can convert it into integer.
Thanks a ton.
As for your problem, there are multiple solutions to it: One is using std::getline as tokenizer. Another is using std::string::find and std::string::substr. Yet another solution is to use the old C function strtok.
The above solutions all give you strings, so either you have to make devId and switchState strings as well, or use e.g. std::stoi to convert the strings to integers.
Apparently the Arduino platform have neither of the functionality listed above, which mean you have to tokenize the string yourself. This is, however, very simple: Find the separator (the ':' character), copy from the beginning to (but not including) the separator into a temporary string and convert it to an integer with e.g. std::strtol. Then copy from (but not including) the separator into the temporary string and convert that value to an integer.
If you don't even have std::strtol then use std::atoi.
Since this is Arduino, you should probably use the String class since it handles a lot of the annoying stuff for you like conversion to int without having to explicitly link against glibc.
You could split the string like so:
String receivedPayload("01:10");
String devId = receivedPayload.substring(0, receivedPayload.indexOf(':'));
String switchState = receivedPayload.substring(receivedPayload.indexOf(':'));
Converting them into an integer would involve just using toInt on the string objects.
If you didn't need to convert into an integer, it would have been easier to tokenise the string yourself by using a for loop and checking if the character is equal to ':' since the string is so short.

Getting an integer value from string in c++

I am trying to learn c++, and for an assignment I have ran into a problem. I am trying to get an integer value from a string that a user enters all on one line.
Ex.) The user inputs: "Change value to 15."
What is the best way of getting the 15 from that string? I have looked around for a while, but could only find if a string was only integers.
Thanks in advance!
Why not use a mixture of getline(grabs your whole line) and string stream(tokenizes the input) and put them all in a vector(easier to use than an array), grab the one at .size()-1 and do an atoi on that. Might be overkill, but string stream could do what you want. For a small tut this could help http://www.dreamincode.net/forums/topic/95826-stringstream-tutorial/
This might not be the best way but to get something done now you can use strtok to tokenize your input string and then examine the tokens for your integer value integer. One of the answers here [link] suggests using strtok to tokenize a string.
If you know the format of you string or know that there will always be a single integer value then you can use string::find_first_of and string::find_last_of [link] and then just get the substring and use string::stoi.

How to know value in the std::string/Cstring is number or not

I am getting command line argument when i launch the application.
I am getting four parameter from command line.
after parsing I store them in four std::string/CString(mfc) variable now i need to know whether the value is decimal or not.because these parameter is going to be used in some mathematical calculation.
Can anybody help me on this.
here you can find how to determine if a string is a numeric, in 70 languages!
I'm not sure if all the solutions check the same thing. The C++ implementation checks if the input is a positive/negative integer/floating-point for base 10, or if it a is positive/negative integer for base 8/16. Is that what you want? Do you need to support only positive numbers? Do you need to support floating-points?
Probably you'll need to convert your input strings to numeric value, so there is no reason to do it in two steps (to check, then to convert). Better do it in a single step.
One more thing: If the input string is too long, for example "32525252332912461984612491264912649126129319312931279171295127951275129" - you normally won't want to consider it as a valid input.
Look at every character in the string, and if you find something that is not a digit, or a '.', then it is not a number.
Just use a string->number conversion function that unambiguously reports failure.
e.g. strtod and not atof