How do i random a number in a string? - string

I´m trying to set a random number in a string, but idk how i can let the program know that i want the random number and not the letter n.
Im using visual studio 2008 , Windows Forms C++
System::Drawing::Font ^Fuente = gcnew System::Drawing::Font("Arial Black",50);
System::Random ^r = gcnew System::Random(System::DateTime::Now.Ticks);
char n=r->Next(1,100);
buffer->Graphics->DrawString("n",Fuente,System::Drawing::Brushes::WhiteSmoke,50,50);,50);

System::Drawing::Font ^Fuente = gcnew System::Drawing::Font("Arial Black",50);
System::Random ^r = gcnew System::Random(System::DateTime::Now.Ticks);
int n=r->Next(1,100);
buffer->Graphics->DrawString(n.ToString(), Fuente, System::Drawing::Brushes::WhiteSmoke,50,50);,50);
Might be what you are after

You're using quotes when drawing string - that's a string literal.
You should convert your number to a string using stdlib.h itoa function.
char number[3];
int n = r->Next(1,100);
itoa(n, number, 10); //number to convert, string to save value in, base
buffer->Graphics->DrawString(number,Fuente,System::Drawing::Brushes::WhiteSmoke,50,50);,50);

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

Arduino: Convert all serial buffer content to string

I am new to Arduino and am a beginner in programming. How do I convert a single char to a string (for example, 'c' to "c") and then append the string "c" to string called data?
Consider the following and see if that helps. I think the Arduino String class is overloaded to do this directly.
char c = 'c';
String data = "something" ;
data = data + c;

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!

Convert all instances of "\" to "\\" in System::String^ and convert to char*

I have an openFileDialog that returns a filename. I want to save that filename as a char* so that I can open it later using fstream. To do this I'll need to replace all instances of \ with \\ in the string.
My tactic for doing this is to split the System::String^ on every instance of \, and then join all the elements of the resulting System::String^ array back together with \\ separating them.
This is the function that I have written, but it seems to return a blank string even when I pass a String^ that contains \.
private: const char* getCharPointer(String^ name){
array<String^>^ words;
String^ delimStr = "\\";
array<Char>^ delimiter = delimStr->ToCharArray( );
String^ replaceDelim = "\\\\";
words = name->Split(delimiter);
String^ tidiedName = String::Join( replaceDelim, words );
label1->Text = tidiedName;
std::string newname=msclr::interop::marshal_as< std::string >( tidiedName);
const char* name_cstr = newname.c_str();
return name_cstr;
}
I am a newcomer to Visual C++ and Windows in general so I would appreciate any pointers on this. It doesn't help that IntelliSense doesn't seem to work with Visual Studio 2010.
Intellisense is not provided in VC++ and It seems it will never be added into VC++.
Anyway, you can use, instead of Join, Replace.
You can see here the reference.
Look at this simple program [ I'm using a Windows Form Application and this code is under a button event handler].
String^ a = "Hello\\World";
String^ b = a->Replace("\\", "\\\\"); //Replace \ with \\
MessageBox::Show(a + " -> " + b);
if you run it, you will get this output.
Hello\World -> Hello\\World
I hope this one is what you need for your code!

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