Converting a string into an integer parameter [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
How to convert a number to string and vice versa in C++
c++ - convert pointer string to integer
Is there a way to convert a string into an integer parameter without any big algorithms?
string = "100";
integerFunction(int string);
I've tried atoi functions and tried to manually convert each number over with the string[count] - 48 way but it needs to be in a way where the number of digits don't become a problem with this. Any suggestions or algorithms out there that can help? I really appreciate it.

Like this:
int StringToInt( const std::string & str )
{
std::stringstream ss(str);
int res = 0;
ss >> res;
return res
}

Related

Converting uint8_t characters to float array

i'm doing a project in C++
the function hx_drv_uart_getchar() only receives 1 uint8_t dtype at a time. how can i convert all received characters from this function into a float array?
the string i am sending over here will be like "-1.2341, 0.9871, 4.5121~" where ~ is to let receiver know its the end of the string
so the characters i am receiving will be something like '-' -> '1' ->'.' ->'2' ->'3' ->'4'-> '1'->','-> so on and so forth
how can i concatenate them and convert it into a float array: arr = {-1.2341, 0.9871, 4.5121}?
the code below is my implementation but acceldata is "45494650515249" after converting. i am suspecting its in ascii form.
so my question is how can i convert received uint8_t data to string and then convert the whole string to a float array? or are there any straightforwards ways? i've been stuck here for a few days already... please advise thank you :)
std::string acceldata;
uint8_t getchar = 0;
hx_drv_uart_getchar(&get_ch);
acceldata = get_ch;
while(get_ch != '~'){
hx_drv_uart_getchar(&get_ch);
acceldata += std::to_string(get_ch);
}

C++: Convert std::string to UINT64

I need to convert a (decimal, if it matters) string representation of a number input from a text file to a UINT64 to pass to my data object.
size_t startpos = num.find_first_not_of(" ");
size_t endpos = num.find_last_not_of(" ");
num = num.substr(startpos, endpos-startpos+1);
UINT64 input;
//convert num to input required here
Is there any way to convert an std::string to a UINT64 in a similar way to atoi()?
Thanks!
Edit:
Working code below.
size_t startpos = num.find_first_not_of(" ");
size_t endpos = num.find_last_not_of(" ");
num = num.substr(startpos, endpos-startpos+1);
UINT64 input; //= std::strtoull(num.cstr(), NULL, 0);
std::istringstream stream (num);
stream >> input;
Use strtoull or _strtoui64().
Example:
std::string s = "1123.45";
__int64 n = std::strtoull(s.c_str(),NULL,0);
There are at least two ways to do this:
Construct a std::istringstream, and use our old friend, the >> operator.
Just convert it yourself. Parse all the digits, one at a time, converting them to a single integer. This is a very good exercise. And since this is an unsigned value, there isn't even a negative number to worry about. I would think that this would be a standard homework assignment in any introductory computer science class. At least it was, back in my days.
you can use stoull:
char s[25] = "12345678901234567890"; // or: string s = "12345678901234567890";
uint64_t a = stoull(s);

Reverse a string with pointers [duplicate]

This question already has answers here:
C++ Reverse Array
(5 answers)
Closed 7 years ago.
This is an amateur question. I searched for other posts about this topic, found lots of results, but am yet to understand the concepts behind the solution.
This is a practice problem in my C++ book. It is not assigned homework. [Instructions here][1] .
WHAT I WOULD LIKE TO DO:
string input;
getline(cin, input); //Get the user's input.
int front = 0;
int rear;
rear = input.size();
WHAT THE PROBLEM WANTS ME TO DO
string input;
getline(cin, input); //Get the user's input.
int* front = 0;
int* rear;
rear = input.size();
Error: a value of type "size_t" cannot be assigned to an entity of type int*
This makes sense to me, as you cannot assign an 'address' of an int to the value of an int.
So my questions are:
What is the correct way to go about this? Should I just forget about initializing front* or rear* to ints? Just avoid that all together? If so, what would be the syntax of that solution?
Why would this problem want me to use pointers like this? It's clear this is a horrible usage of pointers. Without pointers I could complete this problem in like 30 seconds. It's just really frustrating.
I don't really see an advantage to EVER using pointers aside from doing something like returning an array by using pointers.
Thanks guys. I know you like to help users that help themselves so I did some research about this first. I'm just really irritated with the concept of pointers right now vs. just using the actual variable itself.
Posts about this topic that I've previously read:
[Example 1][2]
[Example 2][3]
[Example 3][4]
[1]: http://i.imgur.com/wlufckg.png "Instructions"
[2]: How does reversing a string with pointers works "Post 1"
[3]: Reverse string with pointers? "Post 2"
[4]: Reverse char string with pointers "Post 3"
string.size() does not return a pointer - it returns size_t.
To revert a string try this instead:
string original = "someText"; // The original string
string reversed = original; // This to make sure that the reversed string has same size as the original string
size_t x = original.size(); // Get the size of the original string
for (size_t i = 0; i < x; i++) // Loop to copy from end of original to start of reversed
{
reversed[i]=original[x-1-i];
}
If you really (for some strange reason) needs pointers try this:
string input;
getline(cin, input); //Get the user's input.
char* front = &input[0];
char* rear = &input[input.size()-1];
but I would not use pointers into a string. No need for it.
I guest you may not quite understand the problem here. This problem want you to COPY a C string then REVERSE it by pointer operation. There is no classes in standard C. So, the C string is quite different from string class in C++. It is actually an array of char-type elements ended with character '\0'.
After understand this, you may start to understand the problem here. If you want to copy a C string, you can not just use str_a = str_b. You need constructor here. However, in pure C style, you should REQUIRE memory space for the string at first (you can use malloc here), then copy each element. For example, you want to create a function to make a copy of input string,
#include <string.h>
char *strcopy(char* str_in) {
int len = strlen(str_in);
char *str_out = (char*)malloc(len+1);
char *in = str_in;
char *out = str_out;
while(*in != '\0') { *out++ = *in++; }
return str_out;
}
As you see, we actually use char* not int* here to operate string element. You should distinguish the pointer (such as in) and the element pointed by the pointer (such as *in) at first.
I'll show you a solution in pure C style for your problem, I hope this would help you to understand it. (You should be able to compile it without modification)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* strreverse(char* in){
// length of input string
int len = strlen(in);
// allocate memory for string operation
char *out = (char*)malloc(len+1);
// initialize <front> and <end>
char *front = out, *end = out + len - 1;
char buffer;
// copy input string
for(int i = 0; i <= len; i++){ out[i] = in[i]; }
// reverse string
for(; front < end; front++, end--) {
buffer = *front;
*front = *end;
*end = buffer;
}
return out;
}
int main() {
printf("REVERSE >> %s\n", strreverse("Hello, World!"));
return 0;
}
This is not you would do by C++ in actual programming, however, I guess the problem here is trying to let you understand mechanism of pointers. In this aspect, original C style would help a lot.

Build code: "strlwr" by pointer [duplicate]

This question already has answers here:
Why can't I write to a string literal while I *can* write to a string object?
(4 answers)
Closed 8 years ago.
Someone can tell me different between this code:
char *s1 ="Con Chim Non";
and this one:
char *s=new char[100];
gets(s);
Then, I add the word: "Con Chim Non".
After, I build a code that change value of the pointer. In first code, I got a problem about the address. The second is right. And this is my code:
void Strlwr(char *s)
{
if (s == NULL )
return ;
for (int i = 0; s[i] != '\0'; ++i)
{
if ( s[i] <= 'Z' && s[i] >= 'A')
s[i] = s[i]+32;
}
}
And someone can tell me why the first is wrong.
The first example:
char *s1 ="Con Chim Non";
You declare a pointer to a text literal, which is constant. Text literals cannot be modified.
The proper syntax is:
char const * s1 = "Con Chim Non";
Note the const.
In your second example, you are declaring, reserving memory for 100 characters in dynamic memory:
char *s=new char[100];
gets(s);
You are then getting an unknown amount of characters from the input and placing them into the array.
Since you are programming in the C++ language, you should refrain from this kind of text handling and use the safer std::string data type.
For example, the gets function will read an unknown amount of characters from the console into an array. If you declare an array of 4 characters and type in 10, you will have a buffer overflow, which is very bad.
The std::string class will expand as necessary to contain the content. It will also manage memory reallocation.

c++ convert string to hex [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ convert hex string to signed integer
I allready searched on google but didn't find any help.
So heres my problem:
I have strings that allready contains hex code e.g.: string s1 = "5f0066"
and i want to convert this string to hex.
I need to compare the string with hex code i've read in a binary file.
(Just need the basic idea how to convert the string)
5f
Best regards and thanks.
Edit: Got the binary file info in the format of unsigned char. SO its 0x5f...
WOuld like to have teh string in the same format
Use std:stoi as (in C++11 only):
std::string s = "5f0066";
int num = std::stoi(s, 0, 16);
Online demo
Use stringstream and std::hex
std::stringstream str;
std::string s1 = "5f0066";
str << s1;
int value;
str >> std::hex >> value;
strtol(str, NULL, 16) or strtoul(str, NULL, 16) should do what you need.
strtol
strtoul