I'm trying to get integer or bool from database result this way:
bool tblexist = false;
int t_exists = 0;
tblexist = (bool)sqlite3_column_text(chkStmt, 1);
t_exists = atoi(sqlite3_column_text(chkStmt, 1));
... but no one works.
Expected value from sqlite3_column_text(chkStmt, 1) is always 0 or 1.
But I get error message:
invalid conversion from ‘const unsigned char*’ to ‘const char*’
initializing argument 1 of ‘int atoi(const char*)’
||=== Build finished: 2 errors, 0 warnings ===|
How to solve this and get integer or bool on elegant way?
The first line, trying to convert to bool will always return true, as a string pointer will always be "true" if it's not NULL. If you want to use this there are a couple of ways to handle this:
// 1. Dereference pointer to get first character in string
tblexist = (*sqlite3_column_text(chkStmt, 1) != '0');
// 2. Using string comparison
tblexist = (strcmp(sqlite3_column_text(chkStmt, 1), "0") != 0);
For the second, try this instead:
t_exists = atoi((const char *) sqlite3_column_text(chkStmt, 1));
This is because sqlite3_column_text returns the type const unsigned char * but atoi wants const char *.
First of all, the columns are indexed beginning with zero, so unless the query requested two (or more) columns, sqlite3_column_text(..., 1) returns the second column.
Secondly, the function returns a pointer to a character string, so you have to dereference the value from the pointer, and convert the string there:
const char *data = sqlite3_column_text(chkStmt, 0);
int val = atoi (data); // for ascii representation of a number
atoi or similar conversion would be expensive to use.
const unsigned char _TRUE = '1';
const unsigned char* dbDATA = sqlite3_column_text(chkStmt, 1);
bool exists = (_TRUE == dbDATA[0]); //i assume you are expecting "0" or "1" from DB
Related
I am sending a string from RaspberryPi to ESP32 via BT.I am getting an ascii values one per line. How to convert it into a whole one String? I tried as follow but I get an error while running the method printReceivedMEssage(buffer):
invalid conversion from 'uint8_t {aka unsigned char}' to 'uint8_t* {aka unsigned char*}' [-fpermissive]
uint8_t buffer;
void printReceivedMessage(const uint8_t* buf) {
char string_var[100];
size_t bufflen = sizeof(buf);
for (int i = 0; i < bufflen; ++i) {
Serial.println(static_cast<char>(buf[i]));
}
memcpy( string_var, buf, bufflen );
string_var[bufflen] = '\0'; // 'str' is now a string
Serial.print("string_var=");
Serial.println(string_var);
}
void loop()
{
buffer = (char)SerialBT.read();
Serial.println(buffer); // THIS SHOWS AN ASCII VALUES ONE PER LINE
printReceivedMessage(buffer); // ERROR
delay(1000);
}
One error that should be fixed is the incorrect sizeof(). The code is getting the size of a pointer, which in C and C++ does simply that (you will get the value 4 for 32-bit pointers for example), but doesn't return the size of any "contents" being pointed to. In your case, where the buffer contains a null-terminated string, you can use strlen()
size_t bufflen = strlen(buf);
To remove the compiler error you need to pass a byte array to printReceivedMessage(). For example:
uint8_t buffer[200];
Also, you could print the buffer in one call with Serial.println(a_string), but then you need to read a whole string with SerialBT.readString().
The byte array seems to be an unnecessary intermediary. Just read a String over BT, and print it, as in the post I linked to.
Serial.println(SerialBT.readString());
I'm iterating over a char* as follows, but on the line indicated, I'm getting error C2446 '!=' no conversion from char* to int.
int errorLineNumber = 0;
char* json; //this is a long json with multiple \n's in it.
int offset = errorOffset;
char* p = json + offset;
while (*p-- != '\n' && offset--); //check that this gives offset on that error line and not char position of end of last line before error
errorLineOffset = errorOffset - offset; //get offset on error line only
//count lines to json error
long errorLineNumber = 0;
while (*p!= json) //this is error line
errorLineNumber += *p-- == '\n';
I looked at conversion const char to tchar, but it's a little different, and I also looked at conversion const char to int, which I still don't think is the same issue, unless I'm missing something.
That would be great if someone had a good idea what I'm missing. Thanks!
In line
while (*p != json)
you compare a *p which is of type char to json, which, according to the code above should be a pointer, I assume it is of type `const char*. So you should do
while (p != json) ...
I am trying to write my own string class for an assignment, and I was wondering how I should treat the argument of "".
For example, if there is a call of:
s = myString("")
what is the length, and what are the contents of the char* holding the data in my 'myString' class?
The char * passed to you will be a pointer to an "null" terminated list of char's which, most likely, will be a single, immutable char whose value is 0 (or "null").
For example...
const char* s = "";
char value = *s;
int length = strlen(s);
... should result in...
s == [compiler defined]
!value == true
length == 0
I'm trying to extract two numbers separated by a space from a string and save them as two ints. For example:
if input_string = "1 95" then:
servo_choice = 1
input_number = 95
Code:
String input_string = "";
int input_number = 0;
int servo_choice = 0;
input_string = Serial.readString();
sscanf( input_string, "%d %d", &servo_choice, &input_number );
The IDE gives me this error:
exit status 1
cannot convert 'String' to 'const char*' for argument '1' to 'int scanf(const char*, ...)'
Edit: I guess
input_number = input_string.substring(1,5).toInt();
Actually works and does what I want. I'd still like to know how to get sscanf to work if possible.
Thanks for any replies in advance..
You could try to convert your String into a char array with toCharArray and pass that to sscanf. Something like that (not tested though)
int buffer_len = input_string.length() + 1;
char buffer[buffer_len];
input_string.toCharArray(buffer, buffer_len);
sscanf(buffer, "%d %d", &servo_choice, &input_number);
The String is a class, not a basic type. That means that you need a method to convert / return a pointer to char if you want to use it in a sscanf. That method exists and it's called c_str() .
So, your line has to be:
sscanf( input_string.c_str(), "%d %d", &servo_choice, &input_number );
char c;
int array[10][10];
while( !plik.eof())
{
getline( plik, text );
int string_l=text.length();
character_controler=false;
for(int i=0; i<string_l; ++i)
{
c=napis.at(i);
if(c==' ') continue;
else if(character_controler==false)
{
array[0][nood]=0;
cout<<"nood: "<<nood<< "next nood "<<c<<endl;
array[1][nood]=atoi(c); // there is a problem
character_controler=true;
}
else if(c==',') character_controler=false;
}
++nood;
}
I have no idea why atoi() doesn't work. The compiler error is:
invalid conversion from `char` to `const char*`
I need to convert c into int.
A char is already implicitly convertible to an int:
array[1][nood] = c;
But if you meant to convert the char '0' to the int 0, you'll have to take advantage of the fact that the C++ standard mandates that the digits are contiguous. From [lex.charset]:
In both the
source and execution basic character sets, the value of each character after 0 in the above list of decimal
digits shall be one greater than the value of the previous.
So you just have to subtract:
array[1][nood] = c - '0';
atoi() expects a const char*, which maps to a c string as an argument, you're passing a simple char. Thus the error, const char* represents a pointer, which is not compatible with a char.
Looks like you need to convert only one character to a numeric value, and in this case you can replace atoi(c) by c-'0', which will give you a number between 0 and 9. However, if your file contains hexadecimals digits, the logic get a little bit more complicated, but not much.