Values at char* to number - c++

I have the following code:
char msg[10];
int len = acc.read(msg, sizeof(msg), 1); // This will read "R#" where # can be any number.
int number = getNumber(msg);
The function getNumber looks like this:
int getNumber(char* pMsg){
Serial.println(positionMsg);
positionMsg += 1;
Serial.print('[');
Serial.print(positionMsg);
Serial.println(']');
}
This function prints out:
R115
[115]
When the number in the message is 115. How do I return the integer 115?
I come from a Java background, I don't understand pointers.

the simplest would be to use atoi:
http://www.cplusplus.com/reference/cstdlib/atoi/
#include <cstdlib>
int getNumber(char* pMsg){
//assuming format is always R#
return std::atoi((char*)(pMsg+1));
}
Or, you can use atoi directly instead of getNumber...

Function specifies return type of int so we must return an integer.
Using sscanf() allows you to parse/format other data types in one function call.
In C++: using sscanf()
/* sscanf example */
#include <cstdlib>
int getNumber(char* pMsg){
int result;
...
sscanf (pMsg,"R%d",&result);
return result;
}

Related

Locating doubles in a char array (C++/ROOT)

I have parsed some XML data, and I want latitude and longitude information. The line I need to parse is this:
trkptlat="60.397015"lon="5.32299"
This is an element in a char array, how can I extract/parse the numbers as doubles? Note that the number precision varies as the data goes on, so I can't solely rely on picking out the column indexes.
You are using C-Style char arrays as strings. So, my assumptions that you still are on C. Otherwise, you would std::string. Ìn C++ there is no jsutification to use a char array instead of a std::string.
Please see the C-Style solution:
#include <cstring>
#include <cstdlib>
#include <cstdio>
int main() {
char data[] = "SomeTrashMoreTrashtrkptlat=\"60.397015\"lon=\"5.32299\"MoreTrashMoreTrash";
char firstDoubleIndicator[] = "trkptlat=\"";
char secondDoubleIndicator[] = "\"lon=\"";
double latitude = 0;
double longitude = 0;
char* startPosition = strstr(data, firstDoubleIndicator);
if (startPosition) {
latitude = std::atof(startPosition + std::strlen(firstDoubleIndicator));
}
startPosition = strstr(data, secondDoubleIndicator);
if (startPosition) {
longitude = std::atof(startPosition + std::strlen(secondDoubleIndicator));
}
std::printf("\nlatitude:\t%f\nlongitude:\t%f\n\n", latitude, longitude);
return 0;
}
This can be done in numerous ways, warning totally untested code ahead
Using a safe version of sscan with a parameter that looks something like this
"\"trkptlat=\"%d\"lon=\"%d\""
Be sure to check the return value for length and errors.
Using std::find_first_of with digits and with dots
auto start = find_first_of (haystack.begin(), haystack.end(), digits.begin(), digit.end());
auto end = find_first_of (it, haystack.end(), digitsdot.begin(), digitdot.end(),
[](char a, char b){ return a != b; });
double lat = atof(start); // somewhere there might be a version that returns how many chars read also.
// check for errors
etc.
see further http://www.cplusplus.com/reference/algorithm/find_first_of/
if your the trusting kind you can take some shortcuts that you know that
"trkptlat="
will be prepended so you can start at
auto start = haystack+preLen;

Convert (comma separated hex) String to unsigned char array in Arduino

The response payload of my http request looks like this (but can be modified to any string best suitable for the task):
"{0X00,0X01,0XC8,0X00,0XC8,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,}"
How do I turn it into an unsigned char array containing the hex values like this:
unsigned char gImage_test[14] = { 0X00,0X01,0XC8,0X00,0XC8,0X00,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,}
Additional information: The length of the payload string is known in advance and always the same. Some partial solution I found can't be directly applied due to the limitations of the wrapper nature of Arduino for c++. Looking for a simple solution within the Arduino IDE.
Use sscanf("%x", ...), here an example of just 3 hex numbers:
const char *buffer = "{0X00,0X01,0XC8}";
unsigned int data[3];
int read_count = sscanf(buffer, "{%x,%x,%x}", data, data+1, data+2);
// if successful read_count will be 3
If using sscanf() (#include <stdio.h>) is within your limitations then you can call with it "%hhx" to extract each individual hex value into an unsigned char like this:
const int PAYLOAD_LENGTH = 14; // Known in advance
unsigned char gImage_test[PAYLOAD_LENGTH];
#include <stdio.h>
int main()
{
const char* bufferPtr = "{0X00,0X01,0XC8,0X00,0XC8,0X00,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF}";
for (int i = 0; i < PAYLOAD_LENGTH && sscanf(bufferPtr + 1, "%hhx", &gImage_test[i]); i++, bufferPtr += 5);
return 0;
}

Using strstr in C MBED, printing result

I'm trying to compare two char arrays and print to the terminal whether or not the string was found.
When I run my code, the output printed returns a load of jibber that's not related to what I specified. I think more memory than I've specified is being printed but I'm not sure why.
Strstr returns a pointer to the beginning index of the found string (if found), null if not. I'm guessing this is what is causing the error - but I thought by only checking if the result was null rather than printing the result would bypass this.
My code:
include "mbed.h"
include "string.h"
char input[] = "Hello mbed";
char value[] = "llo";
int main() {
char * output;
output = strstr(input, value);
bool found = false;
if (output != NULL) {
found = true;
}
printf(found ? "true" : "false");
}
My output:
trueloHello mbed½mà$Õ[F!FðMøDà(ÛÝéBÝ#\à0 ZFGñ##Ñ. ZFGmºñªñ
ÝÜàøZFGm¸ñ¨ñôÜ[F!F«æ-+ -éðAF%FFÔà9F °GmdùÕ(F½èð-éðAF%FFÈÕ0’à ‘ÕàAF8F°GmdùÕ(F½èðJh*Ð
hSpHh#HpGðµF°F2¡ü÷Èø(¿%0OÐWø%H±m-ùÓGà-IhB
`°ð½-?Ò x:(Ð!FhFþ÷ýhFþ÷mý(3ÐhFþ÷mý(hF Ðþ÷Uý#àjF¡ Fü÷Åøàþ÷?ý³ð¿$пð¿Dôtð¿DôdFhFþ÷UýF0h”Fh0FG(¿Gø%è°ð½Oðÿ0°ð½ð¿$ÛÑð¿ $$Õç:ttl :%p(¼¿ pGJëPø.......
here is a corrected code that cleanly compiles, and works as desired
Note the way the printf() parameters are being set
Note the corrected list of #include statements
//include "mbed.h"
#include <stdio.h> // printf()
#include <string.h> // strstr()
#include <stdbool.h> // bool, true, false
char input[] = "Hello mbed";
char value[] = "llo";
int main( void )
{
char * output = strstr(input, value);
bool found = false;
if (output)
{
found = true;
}
printf( "%s\n", ((found)? "true" : "false" ));
}
There's a problem with the compiler and how it works with const strings. Add the NULL character to the strings to see if it stops printing...
printf(found ? "true\0" : "false\0");
printf() might be a macro on your compiler/library/IDE. Including the header file, ensures that it works as expected. Also, on embedded, main() should never return

Display an int variable in a MessageBox

I am working on an old app written in Visual C++ 6.0. I am trying to display an int variable in a MessageBox for debugging reasons. Here is my code, I thought this would be a simple process, but I am just learning C++. The two lines that are commented I have tried as well with similar errors. Below is the error I am getting.
int index1 = 1;
char test1 = index1;
// char var1[] = index1;
// char *varGo1 = index1;
MessageBox(NULL, test1, "testx", MB_OK);
error C2664: 'MessageBoxA' : cannot convert parameter 2 from 'char' to 'const char *'
Why bother with C-style strings if you tagged C++?
Although Mark Ransom provided MFC solution (which is perfectly valid), here is a Standard C++ one:
int index1 = 1;
std::string test1 = std::to_string(index1);
MessageBoxA(NULL, test1.c_str(), "testx", MB_OK);
References:
std::to_string();
Arrays are evil
Use boost::format for more sophisticated formatting.
int index1 = 1;
char buf[10];
itoa(index1,buf,10);
MessageBox(NULL,buf,"Caption",MB_OK);
Can try this
CString str1;
str1.Format(_T("%d"), index1);
MessageBox(NULL, str1, "testx", MB_OK);
CString's Format works just like printf to populate the string with the parameter list.
The second parameter of MessageBox needs to be a pointer to a string of chars, terminated with NULL. Passing a char will not work.
But, learning to use a debugger is an integral part to learning a language. Why not build a debug build and set a breakpoint on char test1 = index1; instead? You do that by pressing F9 when the cursor is on that line.
For what it's worth, I prefer to use a manipulator for this:
#include <sstream>
#include <iostream>
#include <windows.h>
using std::ostringstream;
using std::ostream;
ostream &msg_box(ostream &s) {
ostringstream &os = dynamic_cast<ostringstream &>(s);
MessageBox(NULL, os.str().c_str(), "testx", MB_OK);
return s;
}
int main() {
ostringstream msg;
msg << "The number is: " << 10 << msg_box;
return 0;
}
This maintains (mostly) the same interface nearly everybody's already accustomed to with iostreams, avoids the type-unsafe CString::Format, and avoids having several lines of distraction everywhere you're going to display a little information for debugging. The other obvious good point is that if you've overloaded operator<< for your own type, that overload will work with this as well.
Acording to your error, you should declare a const pointer on the second parameter.
Like this,
const char * test1= new char();
or use
std::string test1= "";
MessageBox(NULL, test1.c_str(), "testx", MB_OK);
Also using just "Text" will work.
Here is the pure C solution using sprintf method to store all input in buffer and passing that buffer to MessageBox.
#include <stdio.h>
#include <windows.h>
int main(void)
{
int intVal = 50;
float fltVal = 5.5;
char *str = "Test String";
char buf[1024] = {'\0'};//buffer to store formatted input.
//convert formatted input into buffer.
sprintf(buf,"Int value : %d\nFloat value : %f\nString : %s\n",intVal,fltVal,str);
//display whole buffer.
MessageBox(NULL,buf,"INFO",MB_ICONINFORMATION);
return 0;
}

Creating Multiple folders with mkdir() Function using loop c++

I want to create folders in a directory by naming them in a sequence like myfolder1, myfolder2. i tried doing it with mkdir() function using a for loop but it doesn't take 'integer variables' and only takes 'const char values'. what to do now? is there any other function which do that or can mkdir() do that?
I'm not aware of any library calls that take an integer like you are asking. What you need to do is embed the number into the string before passing it to mkdir(). Since you tagged this question with 'c++' I've demonstrated a C++ oriented way of accomplishing this below.
#include <sstream> // for std::ostringstream
#include <string> // for std::string
const std::string baseFolderName = "myfolder";
for (int i = 1; i < 20; ++i)
{
std::ostringstream folderName;
folderName << baseFolderName << i;
mode_t mode = 0; //TBD: whatever is appropriate
mkdir(folderName.str().c_str(), mode);
}
If you really want this, you can use itoa(...)
Lets say
i = 20;
char buffer [33];
itoa (i,buffer,10); //10 means decimal
Now buffer = "20\0"
After this conversion you can add buffer to your default string.
So, all in all, you can use:
std::string str = "string";
char buffer[33] ;
itoa(20, buffer, 10);
str.append(buffer);
mkdir(str.c_str());