Go Down

Topic: Converting char to unsigned int? (Read 1 time) previous topic - next topic

exiva

Is it possible to convert a char into an unsigned int?

I have this bit of code (Needs the webduino library)
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <WebServer.h>

static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
static uint8_t ip[] = { 192, 168, 1, 97 };

#define PREFIX ""
#define NAMELEN 32
#define VALUELEN 500
WebServer webserver(PREFIX, 80);

void testCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete) {
  char name[NAMELEN];
  char value[VALUELEN];
  server.httpSuccess();
  if (type == WebServer::POST)  {
    server.readPOSTparam(name, NAMELEN, value, VALUELEN);
    if(strcmp(name,"type") == 0) {
      if(strcmp(value,"test") == 0) {
        while (server.readPOSTparam(name, NAMELEN, value, VALUELEN)) {
          if(strcmp(name,"data") == 0) {
            server.print("data: ");
            server.print(value);
            //value needs to be an unsigned int.
          }
        }
      }     
    }
  }
}


void setup() {
  Ethernet.begin(mac, ip);
  webserver.addCommand("test", &testCmd);
  webserver.begin();
}

void loop() {
  char buff[64];
  int len = 64;
  webserver.processConnection(buff, &len);
}


The request made to the server can be done with cURL using this
Code: [Select]
curl -d "type=test&data=9050,4600,43350,2150,600,2150,550,4400,550,4450,550,2150,550,2200,550,2150,550,2200,550,2200,550,2150,550,2200,550,2150,550,4450,550,4400,550,2200,550" http://192.168.1.210/test

When the cURL request is made, I need to take value in the string compare on line 20 and make it an unsigned int with the data payload from the post. I'm not sure how I would get the data payload converted to an unsigned int... or if it's even possible.

Anyone help?

majenko

You can convert a char array to an integer using atoi:

Code: [Select]

char *in = "4793";
int out = atoi(in);


However, that creates a signed integer between -32768 and 32767.  For unsigned I would suggest using its sister function "atol" to convert to a signed long, then cast that back to an unsigned int.  That way the long can store the numbers greater than 32867, and the casting will retain anything between 0 and 65535.

Code: [Select]

char *in = "49283";
long temp = atol(in);
unsigned int out = (unsigned int)temp;

Coding Badly


strtoul (or strtol) will not only convert but it will help scan.

exiva

This only gets me the first number up to the , "9050" What I'm trying to get (maybe I didn't explain it right.) is instead of having

Code: [Select]

unsigned int value[] = {9050,4400,600,4400,550,4400,550,2200,550,4400,550,2200,550,2150,550,2200,550,2150,600,2150,550,2200,550,2150,550,2200,550,4400,550,2200,550,4400,550,2200,550};


in my arduino project I want to get it from the value in the http post.

majenko

Then you're asking completely the wrong question.  We have answered the question you asked with the right answer.

You should be asking about string parsing, which is a completely different topic (and something already heavily covered in the forum).

exiva

Oh. I see now what you mean... Thank you for your help and clearing that up.

andyboutte

exiva did you ever find a solution to your problem?

Go Up