Offline
Newbie
Karma: 0
Posts: 32
|
 |
« on: April 14, 2012, 10:39:06 pm » |
I'm new to Arduino and Processing and am having a hard time doing what appears to be trivial. I'm trying to convert a string like this "189148246403325952" to an int. Here are a couple things I've tried which don't work or work partially : since_id = int(since_id_str); //doesn't work since_id = stringToNumber(since_id_str); //this doesn't work...189148246403325952 outputs as 8192 since_id = getInt(since_id_str); //this kind of works but doesn't convert full int...189148246403325952 outputs as 1891 some snippets I've found that should do the conversion: int stringToNumber(String thisString) { int i, value = 0, length; length = thisString.length(); for(i=0; i<length; i++) { value = (10*value) + thisString.charAt(i)-(int) '0'; } return value; } int getInt(String text) { char temp[6]; text.toCharArray(temp, 5); int x = atoi(temp); return x; } One caveat of this conversion is that the length of since_id_str may be dynamic, and the length can't be hard coded. Any ideas how to tackle this?
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 73
Posts: 6841
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #1 on: April 14, 2012, 10:45:28 pm » |
But that huge number doesn't fit into an int, so it can't be converted to one.
______ Rob
|
|
|
|
|
Logged
|
|
|
|
|
Austin, TX
Offline
Faraday Member
Karma: 41
Posts: 5176
CMiYC
|
 |
« Reply #2 on: April 14, 2012, 10:47:47 pm » |
189,148,246,403,325,952 is a slightly larger than the maximum 65,535 which an int can hold. Long is too small as well. You would need to store this value as a float.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Online
Shannon Member
Karma: 219
Posts: 13898
Lua rocks!
|
 |
« Reply #3 on: April 14, 2012, 11:05:28 pm » |
Actually, 32767 is the largest number you can put into a signed int. I ported the "big number" library to the Arduino: http://www.gammon.com.au/forum/?id=11519Using that you can store: 52! = 80658175170943878571660636856403766975289505440883277824000000000000 But you have to work in the "big number" type. But maybe you want the float type. That holds large numbers, just not with full precision. Perhaps if you described what you are trying to do?
|
|
|
|
|
Logged
|
|
|
|
|
Monterrey, N.L. México
Offline
Full Member
Karma: 1
Posts: 154
Model Railroading & Arduino are Fun
|
 |
« Reply #4 on: April 15, 2012, 12:46:50 am » |
I ported the "big number" library to the Arduino: Neat, Nick! 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #5 on: April 15, 2012, 02:12:19 pm » |
Thanks for all the feedback...I should have realized that int had size limitations. How would I go about converting the giant string to a float? I tried this: float since_id = atof(since_id_str); but 189371928857935872 traces as 0.00 These large number strings are IDS of tweets that I'm parsing from XML: https://dev.twitter.com/docs/api/1/get/searchI'm using this since_id data to scrape for tweets that have been created since my last GET query. I could probably store this data as a String/Char, but I figure I should learn how to do this conversion. Any ideas?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #6 on: April 15, 2012, 02:23:28 pm » |
A float isn't going to be any better - still only 32 bits. Save the ID as a string, or a set of BCD digits will save 50% of storage.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Global Moderator
Melbourne, Australia
Online
Shannon Member
Karma: 219
Posts: 13898
Lua rocks!
|
 |
« Reply #7 on: April 15, 2012, 05:43:53 pm » |
I'm using this since_id data to scrape for tweets that have been created since my last GET query. I could probably store this data as a String/Char, but I figure I should learn how to do this conversion.
Don't bother. You aren't going to be adding 5 to it are you? It's a string, as AWOL said. Store it as one.
|
|
|
|
|
Logged
|
|
|
|
|
West Des Moines, Iowa USA
Offline
Sr. Member
Karma: 2
Posts: 429
|
 |
« Reply #8 on: April 15, 2012, 07:17:24 pm » |
You were on the right track, but needed to use an unsigned long long to hold the 8-byte result. A little tinkering produced #include <stdio.h> int main(void) { unsigned long long x = 0; char *c = "189148246403325952", *p = c; unsigned char b[32]; int i; while (*p) x = 10 * x + *p++ - '0'; printf("x = %llu = %llX\n",x,x); i = 0; do b[i++] = x & 255; while (x >>= 8); do printf("%2.2X ",b[--i]); while (i); puts(""); return 0; } ~/prj/fus/src/Samples: gcc ulltest.c -o ull ~/prj/fus/src/Samples: ull x = 189148246403325952 = 29FFD53F4C22000 02 9F FD 53 F4 C2 20 00
|
|
|
|
|
Logged
|
There's always a better way!
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #9 on: April 16, 2012, 10:32:44 am » |
I'm storing my IDs as a string/char now: class variables are define like so: char current_since_id_str[200] = "0"; <---this is my default value that I use for my initial GET request char next_since_id_str[200]; After I make my GET call I store the response value here (189371928857935872): if((finder.getString("<since_id>","</since_id>",next_since_id_str,200)!=0)); Now this is where I'm getting stuck...I want to set my current id to my next id. I tried this: current_since_id_str = next_since_id_str; But I get an invalid array assignment error. Clearly I'm a a noob at String manipulation. What am I doing wrong?
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 73
Posts: 6841
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #10 on: April 16, 2012, 10:49:54 am » |
Clearly I'm a a noob at String manipulation. You are not working with Strings here but arrays of characters that are often referred to as strings (if the array is NULL terminated.) So a straight assignment will not work, have a look at the strcpy() function if you have NULL termination which I assume you do because you call something called getString(), or memcpy() if not. ______ Rob
|
|
|
|
« Last Edit: April 16, 2012, 10:51:32 am by Graynomad »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #11 on: April 16, 2012, 11:06:22 am » |
I got it working...
Instead of this: current_since_id_str = next_since_id_str;
I loop through the arrays like this: for (int i=0; i <= 200; i++){ current_since_id_str = next_since_id_str; }
I'm finally getting the difference between chars and strings....Is the above solution good performance wise?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #12 on: April 16, 2012, 11:44:21 am » |
Is the above solution good performance wise? No. You have 2 200 byte arrays to hold strings like "189148246403325952". They hardly need to be anywhere near that large.
|
|
|
|
|
Logged
|
|
|
|
|
nr Bundaberg, Australia
Offline
Tesla Member
Karma: 73
Posts: 6841
Scattered showers my arse -- Noah, 2348BC.
|
 |
« Reply #13 on: April 16, 2012, 06:23:25 pm » |
for (int i=0; i <= 200; i++){ current_since_id_str = next_since_id_str; } AKA memcpy(), except memcpy() will work, I don't see how that code will. Try for (int i=0; i <= 200; i++){ current_since_id_str[i] = next_since_id_str[i]; } Is the above solution good performance wise? Ok but can be a bit better char * p_to = current_since_id_str; char * p_from = next_since_id_str; for (int i=0; i <= 200; p_from++, p_to++){ *p_from = *p_to; } Although the compiler may optimise to something like this anyway. EDIT: Oops, as AWOL pointed out, should be < 200 not <= Even better just keep two pointers to the arrays and swap the pointers, that way you don't move any bytes around at all. ______ Rob
|
|
|
|
« Last Edit: April 17, 2012, 07:21:15 am by Graynomad »
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #14 on: April 17, 2012, 01:44:58 am » |
Code:
for (int i=0; i <= 200; i++){ current_since_id_str = next_since_id_str; }
AKA memcpy(), except memcpy() will work, I don't see how that code will. Try You'll notice the post ends in italics. This is what it looks like if you post it correctly, using [code] [/code] tags for (int i=0; i <= 200; i++){ current_since_id_str[i] = next_since_id_str[i]; } Though that code will, of course, read and write beyond the end of 200 element arrays.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
|