How to get second or third 'part' of user input? C++ - c++

User input is:
0 1 4 5
How can i get 0 and save it to a integer, then how can i get 4 and save it to a integer?
Situation 2:
User input is:
0B11B3B76B
How can i save all of them (seperatly) into array (type String)?
I know it's easy question for some of You, but thats my first day in C++, and I have to get this done. .NET forever!

You would have to iterate though each byte of the input string, extracting each digit and casting it as an integer to your array, this helps if you know the size of the input string or the string is a fixed or has a maximum length.
Something like:
char myStr[12] = "0123456789";
int myArray[12];
int intCh;
for (intCh = 0; intCh < 12; intCh++) {
/* Look for the \0 byte that terminates the string. */
if (myStr[intCh] == '\0')
break;
/* We need to cast the char to an int as we store it in the
* array something like this.
*/
myArray[intCh] = (int) myStr[intCh];
}
I hope that helps, I haven't coded in C or C++ for a while but it should give you some pointers...heh, get it? Pointers!
Good luck.
Just to add, your question confused me slightly since you open by saying you want to cast int to char, then you end by saying you want to do the opposite and convert integer types to string.
To do that you'd use the reverse, you'd take the members of your array of integers and cast them to (char) on the assignment:
myStr[intCh] = (char) myArray[intCh];
Or some such thing ;)

Related

How to change int between -120 and 120 to char (or other simple way to save as one byte)

I am reading a sensor and obtaining values for change in temperature every 10 minutes. At the moment I am saving the change in temperature as an integer.
The range in temperature change should be between -120 and 120, I want to save the temperature change to EEPROM, but I only have 512 bytes spare and as an integer the values take up 2 bytes. Therefore I thought I could assign the value to the corresponding char value and save the char to EEPROM (since this will only take one byte) e.g. (e.g. '4' 's' '$' etc.), however I can't see the easy way to do this.
I am using the arduino IDE which is C++ I believe and asking here because it's really a software question
I thought I should be able to use something like
int tempAsInt = -50;
char tempAsChar;
tempAsChar = char(tempAsInt);
or
int tempAsInt = -50;
signed char tempAsChar;
tempAsChar = tempAsInt;
but the first one printed the same characters (upsidedown question mark or null value) for varied tempAsInt values
and the second one just printed out the same value as the integer, i.e. if the change was -50, it printed -50, so I'm not sure if it is really a char, though perhaps I'm just printing it wrong.
My printing code is
mySerial.print("\tTempAsInt: ");
mySerial.print(tempAsInt);
mySerial.print("\tTempDiffAsInt: ");
mySerial.print(tempDiffAsInt);
mySerial.print("\tTempDiffAsChar: ");
mySerial.print(tempDiffAsChar);
In C and C++, there are several ways to cast an object to a different type. Refer http://www.cplusplus.com/doc/tutorial/typecasting/ for a summary.
Without the complete code, it is tough to say for sure. However, your problem seems to be caused by using cout to check the values of the variables like:
cout<<tempAsInt<<endl<<tempAsChar;
cout interprets the tempAsChar variable as a character type and prints the value as per the encoding.
Since -50 is outside the printable range of the ASCII code (refer http://www.asciitable.com/) on your system, you will see some value which is not really a representation of tempAsChar, but a filler such as a question mark for an unprintable.
You can confirm the above behavior by setting tempAsChar to a value of , say, 50 to see the character '2'.
To verify that tempAsChar indeed has the correct value, use printf instead:
printf("tempAsChar (int)= %d and tempAsChar (char)= %c",tempAsChar,tempAsChar);
You should see the output:
tempAsChar (int)= 50 and tempAsChar (char)= 2

Casting an int to a char. Not storing the correct value

I'm trying to store a number as a character in a char vector named code
code->at(i) = static_cast<char>(distribution(generator));
However it is not storing the way I think it should
for some shouldn't '\x4' be the ascii value for 4? if not how do I achieve that result?
Here's another vector who's values were entered correctly.
You are casting without actually converting the int to a char. You need:
code->at(i) = distribution(generator) + '0';
No. \xN does not give you the ASCII code for the character N.
\xN is the ASCII character† whose code is N (in hexadecimal form).
So, when you write '\x4', you get the [unprintable] character with the ASCII code 4. Upon conversion to an integer, this value is still 4.
If you wanted the ASCII character that looks like 4, you'd write '\x34' because 34 is 4's ASCII code. You could also get there using some magic, based on numbers in ASCII being contiguous and starting from '0':
code->at(i) = '0' + distribution(generator);
† Ish.

Converting an integer to to ascii values C++

I am attempting to turn a number into letters using ascii, at the moment I can do it one letter at a time:
EDIT: The output of an RSA encryption that I've been working on is currently in the form of an integer, I'm trying to work out how to convert it to the word/sentence which was the original input. I've nearly finished but I'm completely stuck at the last "hurdle". I'm adding context due to a comment asking why I would want to do this (or words to that effect).
EDIT: If during the encryption process I used the ASCII value - 87, all letters would be 2 digits long, eliminating the problem of some ASCII characters being 3 letters and some being 2, does this make the problem more approachable? (it limits me to only letter but that's fine for its purpose)
#include <string>
#include <iostream>
char returnChar(int x)
{
return (char) x;
}
int main()
{
std::cout << returnChar (119);
}
This converts 32 --> w.
How could I adapt this function to allow me to change "3232" --> "ww" or any other integer to ascii characters, e.g. "32242713" --> "word".
EDIT: I think using some kind of mod function to split it into chunks of two numbers which could then be converted to characters might work?
How do I overcome the problem of some ascii characters having 2 digits and some having 3 digits? I think this problem has been solved as described in the second edit
If you can see that I've approached this in entirely the wrong way, could you suggest a viable alternative approach for me to try please?
Thanks for any feedback.
What you're asking for is not possible. You have a few alternatives:
Change the int to a string and put white spaces/other characters inside the string:
std::string test = "119 119";
Convert the total value to binary, and parse byte by byte:
unsigned int test = 30583; // 119*256+119
char a = (test>>8)&0xff;
char b = test&0xff;
Pass the data in a vector and convert one element at a time:
std::vector<char> returnChar(const std::vector<int> &data){
std::vector<char> output;
for(unsigned int i=0;i<data.size();i++)
output.push_back(char(data[i]));
return output;
}
I would probably stick with the second method, since - a wild guess here - it shouldn't change much things inside where you actually generate the numbers.

C++, Integer and Char array conversion trouble

I have the char array:
char* chararray = new char[33];
and the int:
int exponent = 11111111;
What I want to do, but am confused as to how, is: input the values of exponent into chararray. With the restrictions that exponent has to take up the 2nd to 9th values of chararray. chararray will be all 32 0s,and I want it to become 0xxxxxxxx0000....00, the x's being the 8 digits in exponent.
Furthermore, no build-in conversion functions like atof or atoi. I also want to avoid using Floats or doubles not that you'd really need to.
Note, this is for making IEEE754 32bit values to get some understanding.
Will edit for additional details or clarification if needed.
Try this after initializing the array with '0':
for(int i=9; i>=2; i--) {
chararray[i] = (exponent%10) + '0';
exponent = exponent/10;
}
chararray[32] = '\0';

C++ strings and arrays (very simple)

I am having an extremely difficult time trying to solve this. I would like to know how can I store a string input into an array in C++? I would like the array to be of size 12 because the inputs are going to be binary numbers, so for example this is what I want:
The input is going to be a binary number, 10100 for example, and I want to store that binary number into an array so that the array will look like this --> [1][0][1][0][0]. I want to store in an array any binary number, or, any number of 0's and 1's that the user gives.
the simplest solution is to use the c_str() function of c++ string. This will create a null terminated array of characters that's the same as your original string (you can just ignore the last byte)
so if you have the string myString
char * byteArray = myString.c_str()
will produce the above array
keep in mind that you can also just reference strings using []
string myString = "1101"
//option 1
char firstBit = myString[0];
//option 2
const char * primitiveArray = myString.c_str();
char firstBitOther = primitiveArray[0];