String to Int Conversion in Arduino - string

I' am trying to convert string to int(like Integer.parseInt() in java) in arduino in order to make some operation's on the numbers. Unfortunately none of my solution's worked.
Until now I tried:
Create char Array and call atoi function:
String StringPassword;
uint8_t *hash;
//Here I define hash
int j;
for (j = 0; j < 20; ++j) {
StringPassword.concat(hash[j]);
}
//Checking String Size
Serial.println("Size");
//Checking String
Serial.println(StringPassword.length());
Serial.println(StringPassword);
int jj;
char PasswordCharArray[StringPassword.length()];
StringPassword.toCharArray(PasswordCharArray, StringPassword.length());
awa = atoi(PasswordCharArray);
Serial.println(awa);
Output:
Size
48
168179819314217391617011617743249832108225513297
18209
Create char Array for null terminated string and call atoi function:
String StringPassword;
uint8_t *hash;
//Here I define hash
int j;
for (j = 0; j < 20; ++j) {
StringPassword.concat(hash[j]);
}
//Checking String Size
Serial.println("Size");
//Checking String
Serial.println(StringPassword.length());
Serial.println(StringPassword);
int jj;
char PasswordCharArray[StringPassword.length()+1];
StringPassword.toCharArray(PasswordCharArray,StringPassword.length()+1);
awa = atoi(PasswordCharArray);
Serial.println(awa);
Output:
Size
48
168179819314217391617011617743249832108225513297
-14511
use toInt Function:
String StringPassword;
uint8_t *hash;
//Here I define hash
int j;
for (j = 0; j < 20; ++j) {
StringPassword.concat(hash[j]);
}
//Checking String Size
Serial.println("Size");
//Checking String
Serial.println(StringPassword.length());
Serial.println(StringPassword);
awa = StringPassword.toInt();
Serial.println(awa);
Output:
Size
48
168179819314217391617011617743249832108225513297
-14511
What is the proper way of changing String to Int so:
awa = 168179819314217391617011617743249832108225513297 ?
And could someone explain to me why my solution's didn't worked? I tried to use the function's that were mentioned on Stackoverflow and Arduino forum to solve this.

The number 168179819314217391617011617743249832108225513297 reaches the maximum integer value limit so therefore this will not convert into an integer.
Try using atol() instead of atoi(). Long numbers can hold more data like the number shown above.

Related

Swap two words in a char* using constant space in less than o(n^2) time

Given a char* to a string, say containing "hellosir", and the length's of each word,
how can I return a char* containg "sirhello" using constant space in less than o(n^2) time?
(I was asked this in an tech interview)
Function signature:
char* swapWords(char* str, int firstWordLen, int secondWordLen);
In the "hellosir" example firstWordLen is 5, and secondWordLen is 3.
Thanks
char* swapWords(char* str, int firstWordLen, int secondWordLen) {
char* newStr = (char*) malloc(sizeof(char) * (firstWordLen + secondWordLen + 1));
for(int i = 0; i < secondWordLen; i++) {
newStr[i] = str[firstWordLen + i];
}
for(int i = 0; i < firstWordLen; i++) {
newStr[secondWordLen + i] = str[i];
}
newStr[firstWordLen + secondWordLen] = 0;
return newStr;
}
Explanation:
Loop over the second word, it starts at the end of word one, so firstWordLen + i, and place the chars at the beginning of the new char array
Loop over the first word, so from 0 to firstWordLen and place it at the end of the previously placed second word, so at secondWordLen + i
Add 0 to the end of the new char array because of null termination
Ah ok, this is not constant space my bad, the speed is O(n), but the space is also O(n)

Caesar problem code generating "error: implicitly declaring library function 'strlen' with type 'unsigned long (const char *)'

I am doing the CS50 course and am on week 2. One of the problems of week 2 is called "Caesar". Essentially you have to write code which cyphers text by shifting letters that use the users inputted preferred number. After running my code I keep getting this error
"error: implicitly declaring library function 'strlen' with
type 'unsigned long (const char *)'
[-Werror,-Wimplicit-function-declaration]
for(i = 0, l = strlen(text); i < n; i++)"
This is the code:
int main(int argc, string argv[])
{
string n = argv[1];
int y = argc;
int key = get_int("./caesar ");//getting the number from the user
int k = (key);//assigning key a variable name.
string text = get_string("plaintext: ");//letting the user input their text.
if (key < 1)//Trying to make limit for acceptable input.
{
printf("ERROR");
return 1;
}
int l;
int i;
//for loop containing the encipher process
for(i = 0, l = strlen(text); i < n; i++)
{
if(isalpha(i))
{
if (isupper[i])
{
printf("ciphertext: %c",(text[i] + k)%26 + 65);
}
else (islower[i])
{
printf("ciphertext: %c",(text[i] + k)%26 + 65);
}
}
}
printf("ciphertext: %c", d || c);
return;
int checking_key(int y,string n)
int num = argc;
string key = y;
int num_key = atoi(key);
if(argc != 2)
{
return 0;
}
else
{
if (num_key > 0)
{
return num_key;
}
else
{
return 0;
}
}
}
From man strlen:
Synopsis
#include <string.h>
size_t strlen(const char *s);
Just like one needs to "include" cs50.h to use any of the get_* functions, string.h must be "include"d to access its functions, eg strlen.
Additionally (per comments):
The "ordered comparison" in the compile error
ordered comparison between pointer and integer ('int' and 'string' (aka 'char *')) [-Werror] for(i = 0, l = strlen(text); i < n; i++)
is i < n. Error says one of them is an int and one of them is a string.
On closer inspection this program is a long way from a clean compile. Recommend you follow along with the spec and "approach this problem one step at a time"

garbage in loop for no reason

i wrote a function that receives a string as a char array and converts it to an int:
int makeNumFromString(char Str[])
{
int num = 0, len = 0;
int p;
len = strlen(Str);
for (p = 0; p<len; p++)
{
num = num * 10 + (Str[p] - 48);
}
return num;
}
the problem is that no matter how long the string i input is, when "p" gets to 10 the value of "num" turns to garbage!!!
i tried debbuging and checking the function outside of the larger code but no success.
what could be the problem and how can i fix it?
THANKS
Perhaps your int can only store 32 bits, so the number cannot be higher than 2,147,483,647.
Try using a type for num with more storage, like long.

How to use a set of numbers as the Key for RCA Encryption

i would like to know how can i use a set of numbers as a KEY for the rc4 encryption.
According to the internet and wiki the KEY is actually a string of letters but the bytes are used . But in my program i need to use a 6 digit number as a KEY. Should i covert it to a string or how.
Key Sheudling Algorithm is indicated below.
void ksa(u_char *State, u_char *key) {
int byte, i, keylen, j=0;
keylen = (int) strlen((char *) key);
for(i=0; i<256; i++) {
j = (j + State[i] + key[i%keylen]) % 256;
swap(&State[i], &State[j]);
}
How can i modify the code or should i just convert the numbers to string.
Strings and numbers are both bytes. Here is a working RC4 code that accepts a key of unsigned chars:
#include<stdio.h>
#include<string.h>
#define SIZE 256
unsigned char SBox[SIZE];
int i;
int j;
void initRC4(unsigned char Key[]);
unsigned char getByte(void);
void initRC4(unsigned char Key[])
{
unsigned char tmp;
unsigned char KBox[SIZE];
for(i=0;i<SIZE;i++)
SBox[i]=i;
for(i=0;i<SIZE;i++)
KBox[i]=Key[i % strnlen(Key,SIZE)];
for(j=0,i=0;i<SIZE;i++)
{
j=(j+SBox[i]+KBox[i]) % SIZE;
tmp=SBox[i];
SBox[i]=SBox[j];
SBox[j]=tmp;
}
}
unsigned char getByte(void)
{
unsigned char tmp;
i=(i+1)%SIZE;
j=(j+SBox[i])%SIZE;
tmp=SBox[i];
SBox[i]=SBox[j];
SBox[j]=tmp;
return SBox[(SBox[i]+SBox[j])%SIZE];
}
First, you initialize the RC4 stream:
initRC4(key);
Then you do:
getByte()
...which always returns 1 byte from the RC4 stream you've set up.
One thing to remember though - a letter in string is not always equal to 1 byte. Same goes for the integers and number symbols in strings. Really, you must read an introduction to computer programming before you mess with ciphers.
Here is a demonstration of how bytes differ in strings in integers:
#include <string>
int main(int argc, char **argv) {
const int n=67898;
const std::string str = "67898";
const int arrayLength = sizeof(int);
const int stringArrayLength = str.size();
unsigned char *bytePtr=(unsigned char*)&n;
printf("Bytes for integer: ");
for(int i=0;i<arrayLength;i++)
{
printf("%X ", bytePtr[i]);
}
printf("\n");
printf("Bytes for string: ");
for(int i=0;i<stringArrayLength;i++)
{
printf("%X ", str.at(i));
}
printf("\n");
return 0;
}
Output:
Bytes for integer: 3A 9 1 0
Bytes for string: 36 37 38 39 38
There will usually be a terminating byte at the end of a string, so you could add +1 byte to string size.

Converting a 1D pointer array (char) into a 2D pointer array (char) in Visual C++.

I am new to c++ programming I have to call a function with following arguments.
int Start (int argc, char **argv).
When I try to call the above function with the code below I get run time exceptions. Can some one help me out in resolving the above problem.
char * filename=NULL;
char **Argument1=NULL;
int Argument=0;
int j = 0;
int k = 0;
int i=0;
int Arg()
{
filename = "Globuss -dc bird.jpg\0";
for(i=0;filename[i]!=NULL;i++)
{
if ((const char *)filename[i]!=" ")
{
Argument1[j][k++] = NULL; // Here I get An unhandled
// exception of type
//'System.NullReferenceException'
// occurred
j++;
k=0;
}
else
{
(const char )Argument1[j][k] = filename [j]; // Here I also i get exception
k++;
Argument++;
}
}
Argument ++;
return 0;
}
Start (Argument,Argument1);
Two things:
char **Argument1=NULL;
This is pointer to pointer, You need to allocate it with some space in memory.
*Argument1 = new char[10];
for(i=0, i<10; ++i) Argument[i] = new char();
Don't forget to delete in the same style.
You appear to have no allocated any memory to you arrays, you just have a NULL pointer
char * filename=NULL;
char **Argument1=NULL;
int Argument=0;
int j = 0;
int k = 0;
int i=0;
int Arg()
{
filename = "Globuss -dc bird.jpg\0";
//I dont' know why you have 2D here, you are going to need to allocate
//sizes for both parts of the 2D array
**Argument1 = new char *[TotalFileNames];
for(int x = 0; x < TotalFileNames; x++)
Argument1[x] = new char[SIZE_OF_WHAT_YOU_NEED];
for(i=0;filename[i]!=NULL;i++)
{
if ((const char *)filename[i]!=" ")
{
Argument1[j][k++] = NULL; // Here I get An unhandled
// exception of type
//'System.NullReferenceException'
// occurred
j++;
k=0;
}
else
{
(const char )Argument1[j][k] = filename [j]; // Here I also i get exception
k++;
Argument++;
}
}
Argument ++;
return 0;
}
The first thing you have to do is to find the number of the strings you will have. Thats easy done with something like:
int len = strlen(filename);
int numwords = 1;
for(i = 0; i < len; i++) {
if(filename[i] == ' ') {
numwords++;
// eating up all spaces to not count following ' '
// dont checking if i exceeds len, because it will auto-stop at '\0'
while(filename[i] == ' ') i++;
}
}
In the above code i assume there will be at least one word in the filename (i.e. it wont be an empty string).
Now you can allocate memory for Argument1.
Argument1 = new char *[numwords];
After that you have two options:
use strtok (http://www.cplusplus.com/reference/clibrary/cstring/strtok/)
implement your function to split a string
That can be done like this:
int i,cur,last;
for(i = last = cur = 0; cur < len; cur++) {
while(filename[last] == ' ') { // last should never be ' '
last++;
}
if(filename[cur] == ' ') {
if(last < cur) {
Argument1[i] = new char[cur-last+1]; // +1 for string termination '\0'
strncpy(Argument1[i], &filename[last], cur-last);
last = cur;
}
}
}
The above code is not optimized, i just tried to make it as easy as possible to understand.
I also did not test it, but it should work. Assumptions i made:
string is null terminated
there is at least 1 word in the string.
Also whenever im referring to a string, i mean a char array :P
Some mistakes i noticed in your code:
in c/c++ " " is a pointer to a const char array which contains a space.
If you compare it with another " " you will compare the pointers to them. They may (and probably will) be different. Use strcmp (http://www.cplusplus.com/reference/clibrary/cstring/strcmp/) for that.
You should learn how to allocate dynamically memory. In c you can do it with malloc, in c++ with malloc and new (better use new instead of malloc).
Hope i helped!
PS if there is an error in my code tell me and ill fix it.

Resources