Char to int ?

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.

serial Output
1= 7278978855
2= 55

int NS1 = 7, NS2 = 2, NS3 = 7, NS4 = 8, NS5 = 9;
int NS6 = 7, NS7 = 8, NS8 = 8, NS9 = 5, NS10 = 5;
char res[10]; 
long int fullnumber = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
  sprintf(&res[0], "%01d%01d%01d%01d%01d%01d%01d%01d%01d%01d", NS1, NS2, NS3, NS4, NS5, NS6, NS7, NS8, NS9, NS10);
  
  Serial.print("1= ");
  Serial.println(res);
 
  fullnumber = res[0];

  Serial.print("2= ");
  Serial.println(fullnumber);

  delay(1000);

}

log2 (7278978855) = 32.7611
So, you’re going to need a 33 bit variable.
These aren’t common, so go for 64 bit.

(55 is the ASCII code for the digit ‘7’)

Hi,

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...

Taz.

You try to store 10 integers in 10 chars which is wrong since the size of an integer is not 1 byte...

This doesn't make sense.
A "char" is an integer datatype, just like "int", "short", "long" etc.

AWOL:
This doesn't make sense.
A "char" is an integer datatype, just like "int", "short", "long" etc.

Int size = 2 or 4 bytes : int - Arduino Reference
Char size = 1 byte : char - Arduino Reference

Yes I know that.
So?

What would be a better way to sting the 10 values into one long value ?

What would be a better way to sting the 10 values into one long value ?

Simply not possible.
You need a "long long" - see reply #1

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

So why cant i use this ??????

I refer you to reply #1

(And give your keyboard a shake - your '?' is sticking)

not sure what you mean or what to do there ?

If you really want to Store the one-digit-ints as represanting ascii characters, you could just use

char intAsChar = '0' + originalint

Now i have looked in to string and came up with this.
This works just the way i wanted

Serial output
//0278606849
//278606849

int value1 = 0, value2 = 2, value3 = 7, value4 = 8, value5 = 6;
int value6 = 0, value7 = 6, value8 = 8, value9 = 4, value10 = 9;
long Bigvalue;
String string1, string2, string3, string4, string5;
String string6, string7, string8, string9, string10;
String inString; 

void setup() {
  Serial.begin(9600);

  string1 = String(value1);
  string2 = String(value2);
  string3 = String(value3);
  string4 = String(value4);
  string5 = String(value5);
  string6 = String(value6);
  string7 = String(value7);
  string8 = String(value8);
  string9 = String(value9);
  string10 = String(value10);
}

void loop() {
  string1 += string2 += string3 += string4 += string5 += string6 += string7 += string8 += string9 += string10;
  Serial.println(string1);  
  inString = string1;
  
  Bigvalue = inString.toInt();
  Serial.println(Bigvalue);
  
  while (true);
}

:slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:
If u have a better way of doing this please help
:o :o :o :o :o :o :o :o

Be careful that your leading zero doesn't cause your number to be interpreted as octal.

If u have a better way of doing this please help

Pissing away all those resources is the WORST possible way to do it.

int vals = {0, 2, 7, 8, 6, 0, 6, 8, 4, 9 };
char ltrs[12];
for(int i=0; i<10; i++)
{
  ltrs[i] = vals[i] + '0';
}
ltrs[11] = '\0';
 Serial.println(ltrs);

 Bigvalue = strtol(ltrs, NULL, 10);

thanks Paul but how can the 10 int values be changed separately?

Simply multiply the first ("left most") digit by 10 and add the second digit, then multiply the result by 10 and add the third digit.. etc:

uint64_t value = ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( NS1 * 10ULL ) + NS2 ) * 10ULL ) + NS3 ) * 10ULL ) + NS4 ) * 10ULL ) + NS5 ) * 10ULL ) + NS6 ) * 10ULL ) + NS7 ) * 10ULL ) + NS8 ) * 10ULL ) + NS9 ) * 10ULL ) + NS10 );

Or, you could do that easier with an array and a for loop.

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...

Hope you got my Point

veecue

thanks Paul but how can the 10 int values be changed separately?

int index = 4;
int newVal = 7;
val[index] = newVal;

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);
}