Convert System::String^ to int32 in Visual C++ - visual-c++

I have dataGridView and I need to check dataGridView->Rows[j]->Cells[i]->Value. If its >0 make some work, else - do nothing. I tried
int x = Int32::Parse(dataGridView1->Rows[j]->Cells[i]->Value->ToString());
if(x > 0 )count++;
but have Format exception, Input string was not in a correct format.
int x = Convert::ToInt32(dataGridView1->Rows[j]->Cells[i]->Value->ToString());
also Format exception, Input string was not in a correct format.
int x = safe_cast<int>(dataGridView1->Rows[j]->Cells[i]->Value);
InvalidCast exception, specified cast is not valid.
What I'm doing wrong?
Update: The right way is
double x = Convert::ToDouble(dataGridView1->Rows[j]->Cells[i]->Value);

Related

How to get the 2nd and 3rd character of a String as Integer - C89

So if I have a String
char string[4];
string = "A10";
How Can I get 10 as an Integer.
I tried getting 10 by itself using this but it didn't work.
char string[4];
char string2[2];
string = "A10";
string2[0] = string[1];
string2[1] = string[2];
I don't need to worry about the A, I know how to get that, I need to be able to get the 10 as an integer.
In java you can use the following code to convert string to integer,
int i=Integer.parseInt(s2);
for more information view this website,
https://www.javatpoint.com/java-string-to-int

How to parse Int from String.charAt()

I need to parse Integer from first String position.
Something like this:
String s = "1abc";
int x = s.charAt(0);
This doesn't work (obviously) but hopefully you got the idea.
I also can't use anything like this:
int x = s.substring(0, 1);
Since that would return second character ('a') in this case.
For java you could do
int x = Integer.parseInt(s.substring(0,1));
Check if it works

Convert String to Long using BufferReader

I'm trying to convert my input read Strings to long (Long). I've tried the valueof() and Long.Parslong(String s) but no luck. Im not sure what is going on. I can certainly print the String but cannot convert to Long ):
while ((line = br.readLine()) != null) {
String[] sheet = line.split(cvsSplitBy); //comma as separator
if(isNumeric(sheet[3])){ //parse into integer, only if first col is num
int n = Integer.parseInt(sheet[0]);
int pop = Integer.parseInt(sheet[3]);
System.out.println(sheet[4]);
System.out.println(sheet[4].getClass().getName());
Long lon = Long.valueOf(sheet[4]); //THIS IS WHERE THE ERROR OCCURS
......skip
The Error is the usual NumberFormatException
Exception in thread "main" java.lang.NumberFormatException: For input string: "34.95"
Any ideas? Thanks Aj
Focus on the description of the exception.
For input string: "34.95"
It has a Decimal value, which can't convert into a Long. You could either use Double.valueOf("34.95") or do a validation for non integer values.

Error in using indexOf not finding char in Arduino String

I have some code that I have no clue why it isn't working.
The code takes a serial input in the form of "xxx,yyy,zzz", where digits can range from 1 to 3 in each number. Because of an odd quirk in an app, it needs to be read as a char, then converted to a string to be handled. The intention is to split into 3 ints, red green and blue, from "RRR,GGG,BBB".
Now this works fine when I manually define String str (see commented code), but when I go and enter it from the serial console, it doesn't want to work. It seems to be coming from the indexOf(',') part, as while using Serial.print(c1);, I found that when I manually entered a string, it returned an index of the comma, but when I used the serial console, it returned -1 (not found).
And yes, the entered string into the console is in the correct format of "RRR,GGG,BBB", I've confirmed that by printing both phone and str independently.
while (Serial.available() > 0) {
char phone = Serial.read();
String str = String(phone);
//String str = "87,189,183";
int ln = str.length()-1;
int c1 = str.indexOf(','); //first place to cut string
int c2 = str.indexOf(',',c1+1); //second place
red = str.substring(0,c1).toInt();
green = str.substring(c1,c2).toInt();
blue = str.substring(c2,ln).toInt();
Serial.print(red);
Edit: With the Arduino String class, creating a string from a char is returning more than just one character, eleven in fact.
This:
char phone = Serial.read();
String str = String(phone);
will never create a string in str that has more than 1 character, since that's what you say you want.
This is the code for the Arduino's String(char) constructor:
String::String(char c)
{
init();
char buf[2];
buf[0] = c;
buf[1] = 0;
*this = buf;
}
So clearly your code will create a 1-character long string.
Also, beware of using indexes computed on the full string, on substrings later.
I'm try to guess that you are using these serial API http://playground.arduino.cc/Interfacing/CPPWindows.
while (Serial.available() > 0) {
char buf[12];
int len = Serial.ReadData(buf,11);
String str = String(buf);
//String str = "87,189,183";
int ln = str.length()-1;
int c1 = str.indexOf(','); //first place to cut string
int c2 = str.indexOf(',',c1+1); //second place
red = str.substring(0,c1).toInt();
green = str.substring(c1,c2).toInt();
blue = str.substring(c2,ln).toInt();
Serial.print(red);
If you are using other API like http://arduino.cc/en/Serial/Read you should follow these API where Serial is a Stream and read() return just the first available char.
Code was fixed by using a different function.
while (Serial.available() > 0) {
char phone = Serial.read();
str += phone;
//String str = "87,189,183";
int ln = str.length()-1;
int c1 = str.indexOf(','); //first place to cut string
int c2 = str.indexOf(',',c1+1); //second place
red = str.substring(0,c1).toInt();
green = str.substring(c1,c2).toInt();
blue = str.substring(c2,ln).toInt();
Serial.print(red);
I'm not sure why this works, and why before I was getting a string with more than one character. But it works!

Arduino: String to int gets strange values

I want to convert a String to an int, and all I could find is that you have to convert the String to a char array and then cast this array to an int, but my code produces strange values and I can't figure out what the problem is.
void ledDimm(String command)
{
// Get the Value xx from string LEDDimm=xx
String substring = command.substring(8, command.length());
Serial.println("SubString:");
Serial.println(substring);
Serial.println("SubString Length:");
Serial.println(substring.length());
// Create a Char Array to Store the Substring for conversion
char valueArray[substring.length() + 1];
Serial.println("sizeof ValueArray");
Serial.println(sizeof(valueArray));
// Copy the substring into the array
substring.toCharArray(valueArray, sizeof(valueArray));
Serial.println("valueArray:");
Serial.println(valueArray);
// Convert char array to an int value
int value = int(valueArray);
Serial.println("Integer Value:");
Serial.println(value);
// Write the Value to the LEDPin
analogWrite(LEDPin, value);
}
And the serial output looks like this:
Received packet of size 11
From 192.168.1.4, port 58615
Contents:
LEDDimm=100
SubString:
100
SubString Length:
3
sizeof ValueArray
4
valueArray:
100
Integer Value:
2225
I expected to get an int with the value of 100 but the actual int is 2225?! What have I done wrong here?
There is even an (undocumented) toInt() method in the String class:
int myInt = myString.toInt();
You need to use the function int value = atoi(valueArray); where valueArray is a null terminated string.
The toInt () method is very useful in this aspect, but I found that it is able to convert only strings of length five or less, especially a value less than 65535 as its the maximum value int can take. Over this value, it just gives random numbers (overflowing values). Please be aware of this when you use this method as it killed a lot of my useful time to figure this out. Hope it helps.

Resources