String to const char* in Arduino? - c++

I have a variable tweet that is a string and it has a character at the very beginning that I want to clip off.
So what I want to do is use strstr() to remove it. Here's my code:
tweet = strstr(tweet, "]");
However, I get this error:
cannot convert 'String' to 'const char*' for argument '1' to
'char' strstr(const char*, const char*)
So my thought would be to convert tweet into a char. How would I go about doing so?

string has a c_str() member function that returns const char *.

How about you use substring instead. This will be less confusing than converting between different types of string.
http://arduino.cc/en/Reference/StringSubstring

you can do that easier. Since you're using C++:
tweet = tweet.substring(1);
substr() returns a part of the string back to you, as string.
The parameter is the starting point of this sub string.
Since string index is 0-based, 1 should clip off the first character.
If you want to use strstr you can just cast tweet into a c-string:
tweet = strstr( tweet.c_str(), "]" );
However, that's pretty inefficient since it returns a c-string which has to be turned into a std::string against in order to fit into tweet.

I realize this is an old question, but if you're trying to, say, compare a specific char, and not just one letter in a string, then what you want is string.charAt(n). For example, if you're doing serial programming and you need to check for STX (\02) than you can use the following code.
char STX = '\02'
if (inputString.charAt(0) == STX) {
doSomething();
}

Using the following statement tweet.c_str() will return the string buffer, which will allow you to perform the edit you want.

Look at:
string.indexOf(val)
string.indexOf(val, from)
Parameters
string: a variable of type String
val: the value to search for - char or String
from: the index to start the search from
See this page

Related

Character Pointer and string variable

I tried to use a character Pointer go throw string character (iterate) but I found i can not say the below:
string Name = "Hello";
char *ch = Name;
like the previous statements i am getting error during execution.
However when I am doing like that:
char *ch = "Hello";
the program running without throwing any exception.
Why is that?
I have recently encountered similar problem and the simplest answer is that std::string is a different type from char*, more precisely std::string is an object which contains some characters (your text) and few methods, which allow you to do multiple operations with your text. You can imagine creating a class Integer for storing the value, but also a method allowing you to square and cube the number which is stored in the Ingerer class. Even though they could store the same numerical value, you will not be able to compare them (unless you overload the operator==), as their types are different.
If you wish to use the code you provided, you need to rewrite the second line as
const char *ch = Name.c_str();
it is allowed because std::string contains a method c_str() which "casts" itself to const char*. If you want to learn more about strings, be sure to visit C++ reference about strings.

Opening directory function

This is a basic understanding concepts related question.
Working using: Embarcadero C++ Builder
What is the difference between:
opendir("C:\\XYZ")
and
String file = "C:\\XYZ";
opendir(file);
Aren't both strings?
The first one works but the sexond gives me error:
E2034 Cannot convert Unicode String to ' const char*'
In a case where I take input from the user I can only pass a string. How do i pass the whole path?
first one is a const char*, second one is a std::string. The opendir function accepts only const char* in your case and thus cannot convert std::string to const char* on its own. you can get the function to work by opendir(file.c_str()); .
No. A String is not a char array. opendir needs a char array.
opendir() expects an 8bit narrow const char* as input. When you pass a narrow literal to opendir(), you are passing it a const char[], which implicitly degrades to const char*, and all is fine.
String is System::String, which is a typedef for System::UnicodeString, which is Embarcadero's UTF-16 encoded string class (similar to std::wstring, but with different semantics). When you pass a String to opendir(), you get a conversion error.
To pass a String value to opendir() (or any other function that expects char*), you need to first convert it to a System::AnsiString, and then use AnsiString::c_str() to get a char* from it, eg:
String file = "C:\\XYZ";
opendir(AnsiString(file).c_str());

How does printf display a string from a char*?

Consider a simple example
Eg: const char* letter = "hi how r u";
letter is a const character pointer, which points to the string "hi how r u". Now when i want to print the data or to access the data I should use *letter correct?
But in this situation, shouldn't I only have to use the address in the call to printf?
printf("%s",letter);
So why is this?
*letter is actually a character; it's the first character that letter points to. If you're operating on a whole string of characters, then by convention, functions will look at that character, and the next one, etc, until they see a zero ('\0') byte.
In general, if you have a pointer to a bunch of elements (i.e., an array), then the pointer points to the first element, and somehow any code operating on that bunch of elements needs to know how many there are. For char*, there's the zero convention; for other kinds of arrays, you often have to pass the length as another parameter.
Simply because printf has a signature like this: int printf(const char* format, ...); which means it is expecting pointer(s) to a char table, which it will internally dereference.
letter does not point to the string as a whole, but to the first character of the string, hence a char pointer.
When you dereference the pointer (with *) then you are referring to the first character of the string.
however a single character is much use to prinf (when print a string) so it instead takes the pointer to the first element and increments it's value printing out the dereference values until the null character is found '\0'.
As this is a C++ question it is also important to note that you should really store strings as the safe encapulated type std::string and you the type safe iostreams where possible:
std::string line="hi how r u";
std::cout << line << std::endl;
%s prints up to the first \0 see: http://msdn.microsoft.com/en-us/library/hf4y5e3w.aspx, %s is a character string format field, there is nothing strange going on here.
printf("%s") expect the address in order to go through the memory searching for NULL (\0) = end of string. In this case you say only letter. To printf("%c") would expect the value not the address: printf("%c", *letter);
printf takes pointers to data arrays as arguments. So, if you're displaying a string (a type of array) with %s or a number with %d, %e, %f, etc, always pass the variable name without the *. The variable name is the pointer to the first element of the array, and printf will print each element of the array by using simple pointer arithmetic according to the type (char is 1 or 2 bytes, ints are 4, etc) until it reaches an EOL or zero value.
Of course, if you make a pointer to the array variable, then you'd want to dereference that pointer with *. But that's more the exception than the rule. :)

c++ char question

so I'm implementing this chess program on C++ and I'm trying to integrate to winboard protocol...one of the functions that they say I need to write to do so should have the following signature:
char *MoveToText(MOVE move); // converts the move from your internal format to text like e2e2, e1g1, a7a8q.
my question is....the text formats are something like e2e2....but the return type of that function is char...and as far as I can understand it, char is just one single character....
so how come are they telling me to use this signature?
or am I mistaken and in fact char can also store multiple characters such as e2e2, e1g1 etc?
Yeah, in C, a char* points to an array of characters. C treats arrays of characters as strings, terminated by a null byte.
The return is a char* or a c-style string =)
char * is a pointer on char - address of sequence of characters.
It returns pointer to char, which is basically a c-string.
Take a look at this tutorial: http://www.cprogramming.com/tutorial/lesson9.html

C++ Variable Conversion

I need to pass one of my parameters to a write() function. It is asking for a type of 'const void*' I am a PHP guy and don't know C++ very well.
Here is my parameter:
const fmx::Text& distance = dataVect.AtAsText(3);
I don't know of any other way to pull in that field. I would love to just declare it const void* but I don't know how.
I guess just converting it would be easier than trying to pull it in the correct way??
The error message: cannot convert const fmx::Text to const void* for argument 2
write(fd, distance, 4);
I know this worked so can I just convert?
const void* s = "5000";
This is for a plugin in FileMaker so I don't really get c++ here.
Is there more anyone would need to help me solve this??
Thanks so much!
If fmx::Text was a pointer type, the compiler would automatically convert a reference to it into a void*. You need to use the address-of operator to give the function a pointer to work with:
write(fd, &distance, 4);
I don't really know filemaker, but this link shows that fmx::Text has a GetBytes function. You can then pass the pointer to the buffer filled with this function.
I'm assuming you actually want the text string.
I think you need to check the api for fmx::Text to get the string you want. Here is something I found to get the string out.
Looks like the type stores the data as UTF16, so you have to run a bit of code to get a string out, then pass it to your write function:
//a function to convert to a normal string
std::string getString(fmx::Text& Text)
{
char buffer[512] = {0}; //NOTE YOU HAVE A STRING SIZE LIMIT
// convert original text to ASCII text
outText.GetBytes( buffer, sizeof(buffer)-1, 0, Text.GetSize(), fmx::Text::kEncoding_Native );
return buffer;
}
Then call the function
std::string myString = getString(distance);
write(fd, myString.c_str(), myString.size());
Note I'm assuming a lot here...that you want a string in the current encoding, and not the raw UTF16 data from 'distance'. AND that GetBytes will not mangle the null characters in buffer....
You'll also need to include <string> in your c++ file.