Hi guys im having problems with trying to convert char to int
what im doin is i have 10 int vales then i join them together to make one long number that is working i get 1= 7278978855.... but how can i change this to int to store that value for later use.
You are reading res[0] which contains character 7.
This char is represented by the hex value 0x37 which is 55 in decimal.
That's why you get 55 in full number.
Also why would you want store these integers in an array of char?
You try to store 10 integers in 10 chars which is wrong since the size of an integer is not 1 byte...
Long variables are extended size variables for number storage, and store 32 bits (4 bytes), from -2,147,483,648 to 2,147,483,647
longs unsigned longs won't store negative numbers, making their range from 0 to 4,294,967,295
So why cant i use this ??????
Most of the time ill only need to store 9 numbers... i have use the process before but with complete numbers eg. 948596897. but now i need to string the 9 or 10 numbers together and make one int value
vmansmx5:
thanks Paul but how can the 10 int values be changed separately?
As I said before, if you want to convert any one-digit int to a asci char (part of a string, not char as a number), just use the single code line paul used in his for loop:
char digit_represented_as_char = digit + '0'
If you add 0 to asci character 0 you will get "0"
If you add 1 to asci character 0 you will get "1", because the digits follow in asci
If you add 2 to asci character 0 you will get "2", and so on...
First, the version by the OP uses 7808 bytes of flash memory and 300 of SRAM, evidence of the resource-hungry String class Paul mentions. Paul’s code size is 5902 and 222 bytes of SRAM. Below was my attempt (I wasn’t as fast as Paul), and it comes to 5478 but 236 bytes of SRAM. Because our code is very similar, would strtol() account for the difference??
int values[] = {0, 2, 7, 8, 6, 0, 6, 9, 4, 9};
long Bigvalue;
char inString[12];
void setup() {
char temp[10];
int i;
Serial.begin(9600);
for (i = 0; i < sizeof(values) / sizeof(values[0]); i++) {
itoa(values[i], temp, 10);
strcat(inString, temp);
}
}
void loop() {
Serial.println(inString);
Bigvalue = atol(inString);
Serial.println(Bigvalue);
while (true);
}