Split and Assign char - c++

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.

Related

Is there any way to convert a char type into an int?

I want to add two large numbers in C++ using vectors. However, I can't find any way in which I could properly read them from a file, so I wanted to use a char array which will read the characters and, if they are digits convert them into an int variable of the vector.
Is there any way in which I could do this?
You can easily convert digits into the corresponding integer values, because char can be treated as numeric values. Assuming you got some input like
char input = getNextDigit();
You can do
int asInt = input - '0';
which works, if input is any digit from '0' to '9'.
You can use the atoi function. The given link has pretty clear examples on how to use it.
I hope this is what you were looking for!
UPD: whenever you deal with strings it is better to go one step up in terms of abstraction and don't work with arrays of char directly. std::string should be your first choice. Starting from C++11 atoi has an std::string version called stoi.

Protobuf : C++ string with null characters inside

I am dealing with a buffer in memory which is being read as protobuf. I need to deserialize it. The content of this protobuf contains a string which may or may not have null character inside the string. For example, the string could be something like this : "name\0first". If I have the input like this, the string that I can deserialize always looks like "name" since the string class drops the part after null character.
How can I access the complete string in this case? String length function obviously do not help in this case.
First, you need to construct the string appropriately. You cannot construct it using the constructors that are looking for NULL terminators, which is what string(const char *) is looking for.
You have to use the constructor that takes a pointer and length.
string s("name\0first", 10);
If you have already constructed a string, and want to append data that has embedded NULLs, you can use the append() method.
string s;
s.append("name\0first", 10);
Use the constructor that takes the number of characters std::string s(buffer, nChars). It is the fifth from this reference.

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 determine a character within a string in c++

I have a variable named "String" that may have values like the following ones:
const char* String = "/v1/AUTH_abb52a71-fc76-489b-b56b-732b66bf50b1/test/DSC_0188.JPG";
or
const char* String = "/auth/v1.0";
or
const char* String = "/v2/AUTH_abb52a71-fc76-489b-b56b-732b66bf50b1/images?limit=1000&delimiter=/&format=xml";
Now I want to make sure whether or not "String" has the character 'v1'. Checking this has to be precise. I tried with strchr, but it's not quite accurate as it doesn't take 'v1' as one character, it rather takes 'v' and '1' as two separate characters. Moreover I can't use namepace std and library string, I can only use "string.h". Within these limitations how can I accurately check whether the variable "String" has a character 'v1'?
Thank you.
I want to make sure whether or not "String" has the character 'v1'
I can only use "string.h"
Then you probably want strstr. Also v1 is not a character, it's a string.
Side note: why use cstring in C++ ? What kind of teacher is still calling it string.h ?!
v1 is not character according to any alphabet. This is a proper string "v1" and as #cnicutar mentioned the c way to search for string in string is to use strstr. It is quite easy to use and runs KMP which is also very fast (though for the kind of your string it is not that crucial).
I would advise you to:
always name your variables starting small-caps (i.e. String -> my_string)
declare your string as const char[], no need to interfere with pointers, when you can avoid them. Declaring this as pointer might confuse you, that you dynamically allocated the memory for the string.

c++ char arrays, use in cin.getline()

I have the following code:
char myText[256];
cin.getline(myText,256);
Why exactly do I have to pass a character array to cin.getline() and not a string?
I have read that in general it is better to use strings than character arrays. Should I then convert a character array to a string after retrieving input with cin.getline(), if that is possible?
You are using the member method of istream. In this case cin. This function's details can be found here :
http://en.cppreference.com/w/cpp/io/basic_istream/getline
However you could use std::getline
Which uses a string instead of a char array. It's easier to use string since they know their sizes, they auto grow etc. and you don't have to worry about the null terminating character and so on. Also it is possible to convert a char array to a string by using the appropriate string contructor.
That's an unfortunate historical artifact, I believe.
You can however, use the std::getline free function instead.
std::string myText;
std::getline(std::cin,myText);