basickarl
basickarl

Reputation: 40454

How to do this in C (from Java)

I am currently learning the wonderful Arduino and some C and I am trying to get this to work. How do I do the following in C!

String val = "";
while(true) {
    thisChar = "2"; // this will be a "char" in C, this is finished in C, it's reading from a stream, the "2" is just an example
    if(val.length < 3) {
        val = val + thisChar;
    } else {
        int num = val;
        // i will do something with my new int thing
        val = "";
    }
}

So I am trying to basically get a char, bunch three of them into a string, covert it into an int and then do something with it. the numbers sent in threes are anything between 000 and 100!

I will post what I have come up with.

char val[];
if (client.available() > 0) { //finns åtmminstone 1 klient?
  char thisChar = client.read(); //läser av nästa byte från servern
  if( thisChar == 'H' ){
    Serial.println("HIGH from client");
    digitalWrite(led, HIGH); // lys LED
  }
  else if( thisChar == 'L' ){
    Serial.println("LOW from client");
    digitalWrite(led, LOW); // släck LED
  }
  else {
    Serial.println(thisChar);
    int len = strlen(val);
    if(len < 3) { // saknas fortfarande tecken tex 0 eller 02
      val = val + 
    }
    else { // värdet är komplett tex 010 eller 100
      val = "";
    }
  }
}

ANSWER: Thanks to @morgano for the chat he was able to piece together the following code from all three answers!

  static char val[4] = {0}; //we only care about 3 digit numbers. 
  static int len = 0; 
  //... code blabla
  char thisChar = client.read(); //läser av nästa byte från servern
  //... code blabla
  else { 
    val[len] = thisChar; 
    len++;  
    if(len > 2) { // värdet är komplett tex 010 eller 100 
      int i;
      sscanf(val, "%d", &i);
      Serial.println(i);
      //Serial.println(val);
      len = 0; 
      //val[3] = 0; 
    }
  }

Upvotes: 1

Views: 128

Answers (3)

Yash89
Yash89

Reputation: 71

Converting your Java code directly to C gives me this. Change the array size to what your max string size could be.

char *val;
char inputchar[10];
int num;
val= malloc(sizeof(char) *20);
while(1){
  inputchar=readclient();
  if(strlen(val) < 3)
   val = strcat(val, inputchar);
  else { 
   num= atoi(val);
   memcpy(val, '\0', 10 );
   } 
  }
 free(val);

You need to check the functions atoi and memcpy once.

Upvotes: 1

morgano
morgano

Reputation: 17422

Adding yet another solution that doesn't imply using char[] or "string" functions:

int val = 0;
int len = 0;

while(1) {
    char thisChar = clientread();
    if(len < 3) {
        val = val * 10 + (thisChar - 0x30);
        len++;
    } else {
        do_something_with_val(val);
        val = 0;
        len = 0;
    }
}

Upvotes: 1

apnorton
apnorton

Reputation: 2440

What you have looks good--you just need to decide how to "append" text to your char array.

I would keep a variable describing the current "length" of the string. So, you can try something like:

char val[4]; //we only care about 3 digit numbers. 
int valLength = 0; //No characters in the string yet.

char thisChar = client.read();

val[3] = '\0';  /* Need to terminate the string, or else... */
if (valLength < 3) {
  val[valLength] = thisChar;
  valLength++;
}
else {
  int myIntVal = strtol(val, 0, 10); //I believe this is the right syntax.  I'm not 100% sure.
  val[0] = 0;
  val[1] = 0;
  val[2] = 0;
}

Upvotes: 1

Related Questions