# Converting a 15-character all-numeric string from ASCII to 2-3 unsigned longs

I have a web page which allows a user to enter a number between 0 and 999999999999999. The entered value is then received by the microcontroller as an ascii string. So, I have an ascii sting of varying length and I want to convert it to unsigned long integers, such that the lowest 5 decimal places are in one variable, the middle 5 if they exist are in the second variable and the highest 5 if they exist are in the third variable. I am not even sure where to begin.

var1 = inputValue % 100000;
var2 = (inputValue / 1000000) % 100000;
etc.

1 Like

It's a string...

Can't you use a for loop:
l = string length.
place=1
for (i=0;i<5; i++{
unsigned_long_intege1 += (ascii_string[(l-1)-i] - 48)* place;
place *= 10;
}
etc. for the other 2 longs?

I'm trying to follow you here, but not sure I do...
If the number entered is "564732" how do I get var1 to have the value 64732 and var2 to have the value 5?
If the number is "1234" This one is fairly straightforward, as I just check strlen () <= 5...
If the number is "63453294031" how do I get var1 to have the value 94031, var2 to have the value 34532 and var3 to have the value 6?

Take a look at substring. Better yet, have three input fields on the web page.

Make sure the index is > 0;
second long:
for (i=5;(i<10 && ( (l-1) >= i ); i++{

the zero index array always confuses me so i<10 could be 1 out but you should get the idea.

I don't quite get it...

Luckily, the microcontroller supports long long integers, so I can use strtoull() and then break it up like railroader suggested.

First check to see how many characters you receive.

Then convert the last 5 (or less) to a long.

If more than 5 characters, remove the last 5 characters.

Repeat two more times for the remaining longs.

Well example code:

``````void print1(char s[]){
byte l,i;
unsigned long l1,l2,l3,place;
i=0;
l=0;
while (s[l] != 0){ l++; }
place=1;
l1 = 0;
//unsigned long t =0;
Serial.println();
Serial.print("s: ");
Serial.println(s);
for (i=0;(i<5 && i<l); i++){
l1 += (s[(l-1)-i] - 48)* place;
place *= 10;
}
place=1;
l2 = 0;
for (i=5;(i<10 && i<l); i++){
l2 += (s[(l-1)-i] - 48)* place;
place *= 10;
}
place=1;
l3 = 0;
for (i=10;(i<15 && i<l); i++){
l3 += (s[(l-1)-i] - 48)* place;
place *= 10;
}
Serial.print(F("l1: ") );
Serial.println( l1 );
Serial.print(F("l2: ") );
Serial.println( l2 );
Serial.print(F("l3: ") );
Serial.println( l3 );
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println();
Serial.println(F("Serial connected") );

print1("564732");
print1("1234");
print1("63453294031");

}

void loop() {
// put your main code here, to run repeatedly:

}

``````

And /dev/ttyUSB0

``````
Serial connected

s: 564732
l1: 64732
l2: 5
l3: 0

s: 1234
l1: 1234
l2: 0
l3: 0

s: 63453294031
l1: 94031
l2: 34532
l3: 6

``````
1 Like

Yes. My mistake.
How is it stored? In a char array?

Yes, it's buffered into a char array from the web page submission.

Here is a deliberately clumsy code, to show the principles, but doing the job.

``````char inputArray[] = "123456789098765";//Test sample
long v1, v2, v3;

int makenum(char a)
{
return ( a - 48 );
}
void setup() {
Serial.begin(115200);
Serial.println("v1");
v1 = makenum(inputArray);//LSB var1
Serial.println(v1);
v1 += 10 * makenum(inputArray);
Serial.println(v1);
v1 += 100 * makenum(inputArray);
Serial.println(v1);
v1 += 1000 * makenum(inputArray);
Serial.println(v1);
v1 += 10000L * makenum(inputArray);/MSB var1
Serial.println(v1);
Serial.println("v2");
v2 = makenum(inputArray);
Serial.println(v2);
v2 += 10 * makenum(inputArray);
Serial.println(v2);
v2 += 100 * makenum(inputArray);
Serial.println(v2);
v2 += 1000 * makenum(inputArray);
Serial.println(v2);
v2 += 10000L * makenum(inputArray);
Serial.println(v2);
Serial.println("v3");
v3 = makenum(inputArray);
Serial.println(v3);
v3 += 10 * makenum(inputArray);
Serial.println(v3);
v3 += 100 * makenum(inputArray);
Serial.println(v3);
v3 += 1000 * makenum(inputArray);
Serial.println(v3);
v3 += 10000L * makenum(inputArray);
Serial.println(v3);
}

void loop() {
// put your main code here, to run repeatedly:

}
``````

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.