Converting strings to int and libraries that you can’t add

Convert “const char*” to “int” at build.particle.io for math conversions seems to be a hefty task. It brings in zero when I try.

So I created a webhook “get” to grab a number that is 1 dollars worth of bitcoin. And I can do that and display it just fine.

Here is the url if your interested.
https://blockchain.info/tobtc?currency=USD&value=1

I want to (1/bitcoin_variable)

Bringing in data from a web hook through the myHandler function to be exact.

I tried something simple like in function myHandler just declaring it as int like this but no not that easy.


void myHandler(const char *event, int data) {
  // Handle the integration response
}
          

Of course that would keep me from answering the question of how to covert const char* to int… Easy enough question to tackle you would assume…

Seems like all answers point to #includes that throws errors when adding. While trying to solve the conversion issue.

So the question remains to be narrowed down to just converting (const char* to int) in the web ide “cloud compiler”.

This could be recreated by just using code like…

const char* thisCharNumber = "5";
int thisNumber = atoi(thisCharNumber);
//code of choice to display “thisNumber”

Using things like std or sstream and iostream keeps getting suggested but it doesn’t have the ability to be added…

I’m trying to stick with the cloud compiler at “build.particle.io“ I found some possible code includes that could clear this up using something like Visual Studio Code Particle Workbench but again I’m trying to stick with the cloud web browser only…

Thanks!

You already have one of the possible answers (for the question *).
atoi() is the common C/C++ way to convert C strings to integer.

The cloud compiler is nothing than a C++ compiler and hence that’s the way to go.

The stream options you mentioned require a stream (e.g. Serial) to read from and parse while receiving but when the string is already stored you need to use string functions (like atoi() or sscanf()).

This is not supposed to work. The data is sent by some infrastructure that doesn’t know nor care what your code does with it. So the data will come as const char* and you’ll have to adapt to that, you cannot retroactively cause the cloud to send the data the way you’d like to have it :wink:


*)
However, your link does not provide a string that represents an integer but a floating point value and for that you’d use atof()
Once you got that value calculating the reciprocal of that shouldn’t pose any problem.

2 Likes

Thanks this worked using atof();

I did have to use double rather than int along with it. However that was just to display the cents. Int would be a whole number only. I will add a rounding operation in actual code because it displays more than just two digits after decimal.

The below is a version of what I was able to use.

void myHandler(const char *event, const char *data) {
    //Now I change to an integer and divide while doing it.
     double USDperBTC = 1/atof(data);
    //Now I move it to display somewhere. In this case with the Blynk app on virtual pin 0
     Blynk.virtualWrite(V0, USDperBTC );
}