[英]How can I convert String() into Int() in Processing?
我正在做這個項目,我使用 Arduino 板來讀取心跳脈沖,所以我想從 Arduino 發送到處理只有一個數字(人的 BPM); 這樣我就可以在我的處理代碼中使用該信息來操作我在處理中創建的一些視覺圖像。 我目前的問題是該信息在 String() 中,因此我不能將其用作變量 - Int() 或 float() - 而且我在進行這種從 String 到 Int() 的轉換時遇到了困難)。
這是我目前的代碼:
import processing.serial.*;
Serial myPort; // Create object from Serial class
String val;
void setup () {
size(600,600);
noStroke();
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 115200);
}
float t=0;
void draw() {
background(0);
fill(255);
if ( myPort.available() > 0) { // If data is available,
val = myPort.readStringUntil('\n'); // read it and store it in val
println(val);
}
translate(width/2, height/2);
beginShape ();
//add some vertices...
for (float theta =0; theta <= 2*PI; theta += 0.01){
float rad = r(theta,
2, //a
2, //b
6, //m
1, //n1
cos(t)*0.9+0.7 , //n2
sin(t)*0.9+0.7 //n3
);
float x = rad * cos(theta)*100;
float y = rad * sin(theta)*100;
vertex (x,y);
}
endShape(CLOSE);
t+=0.04;
}
float r(float theta, float a, float b, float m, float n1, float n2, float n3 ) {
return pow(pow(abs(cos(m* theta/4.0) /a), n2) + pow(abs(sin(m* theta/4.0) /b), n3), -1.0/n1);
}
我已經嘗試將 String() 轉換為 Int() 但它似乎不起作用:
Serial myPort;
String val;
int valInt;
void draw ();
if ( myPort.available() > 0) {
val = myPort.readStringUntil('\n');
int valInt = Integer.parseInt(val);
println(valInt);
}
在Processing 中有內置函數int
,它可以將字符串(或浮點值)轉換為整數:
String s = "123";
int x = int(s);
print(x);
根據我對您的問題的理解,我有以下解決方案您可以使用以下代碼將字符串值轉換為 int。
int a = Integer.parseInt("51");
51 是字符串值,您可以將其轉換為 int。
String str = "123";
int i = Integer.parseInt("str");
String str="22";
Int value= Integer.valueOf(str);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.