Pages: [1]   Go Down
Author Topic: Converting char to unsigned int?  (Read 1150 times)
0 Members and 1 Guest are viewing this topic.
Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I have this bit of code (Needs the webduino library)
Code:
#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:
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?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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:
char *in = "49283";
long temp = atol(in);
unsigned int out = (unsigned int)temp;
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 199
Posts: 12768
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Long Island, NY
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

exiva did you ever find a solution to your problem?
Logged

Pages: [1]   Go Up
Jump to: