# separate a "number"

Hello,
I have this huge number(for example:123456789101112131415161) stocked in a char.
I want to separate this number in 5 equals part(like this 1234 ,5678 ,9101 ,1121 ,3141 ,5161.)
The problem is that i can’t transform this huge number into a int or a long because the number is to big Can you help separate this number?
Thank you.

Just count every four digits, and put each set into a different element of an array.

and what do i do next?

Put your feet up and have a beer, content in the knowledge of a job well done?

What do you want to do with that number?
.

Looool
Sorry i've forgot to tell you that i wanted to store every number in a different variable.

If they're in different elements of an array, they're already effectively in different variables.

Ok your solution looks cool.
But how do i do it?(i'm new )

Tozitron:
Ok your solution looks cool.
But how do i do it?(i'm new )

Lets look at what you have said.

I have this huge number(for example:123456789101112131415161) stocked in a char.

You most certainly do NOT have that number stored in a char. The range of values that can be stored in a char is from -128 to 127. That number is just a bit outside that range.

You MIGHT have the digits stored in a char ARRAY, but you haven't posted any code to prove that.

So, you have not provided near enough information.

The ball is in your court.

ok here is the code

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac = {
0x90, 0xA2, 0xDA, 0x0D, 0x24, 0x50
};
IPAddress ip(192, 168, 0, 98);
IPAddress IP_Remote(192, 168, 0, 77);
unsigned int localPort = 12345;
unsigned int Port_Remote = 6101;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; /
char ReplyBuffer = “acknowledged”;
EthernetUDP Udp;
void setup() {
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Serial.begin(115200);
}

void loop() {
int packetSize = Udp.parsePacket();

if (packetSize) {

Serial.println(packetBuffer);
}
delay(5);
}
the number is in packetBuffer

How did you get that number in the first place?

If you start with it like this

``````1234,5678,9101,1121,3141,5161
``````

then you can easily parse it with code like the parse example in Serial Input Basics

...R

ok i do have this now:1234,5678,9101,1121,3141,5161
but i don't know how to separate it

but i didn't find the Serial Input Basics

I did, just by clicking the link.

Look at strtok() and atoi().

Tozitron:
ok i do have this now:1234,5678,9101,1121,3141,5161
but i don't know how to separate it

You seem to have edited this after Reply #12 - that is not a nice thing to do as it makes a nonsense of Reply #12. In a situation like that just make a new Reply.

You have not said if you have studied the parse example or what specific part of it you need help with.

...R

Very simple example of separating a character string similar to yours captured as a String variable. Depending on what you are actually trying to do there may be better ways. Do you have control over how the data is sent?

``````//send string using serial monitor
//like 123456789101112131415161

String readString, data1, data2, data3, data4, data5;

void setup() {
Serial.begin(9600);
Serial.println("parse-test-3"); // so I can keep track of what is loaded
}

void loop() {
while (Serial.available()) {
delay(2);
if (Serial.available() >0) {
char c = Serial.read();  //gets one byte from serial buffer
readString += c; //makes the string readString
}
}

if (readString.length() >0) {
Serial.println();

// expect a string like 123456789 with three data partts
data1 = readString.substring(0, 5); //get the first characters
data2 = readString.substring(5, 10); //get the next characters
data3 = readString.substring(10, 15); //get the next characters
data4 = readString.substring(15, 20); //get the next characters
data5 = readString.substring(20, 25); //get the next characters

Serial.println("parsed data:");

Serial.println(data1);  //print to serial monitor to see results
Serial.println(data2);
Serial.println(data3);
Serial.println(data4);
Serial.println(data5);
Serial.println();

Serial.println("data converted to numbers:");
int n1 = data1.toInt();
Serial.println(n1);
int n2 = data2.toInt();
Serial.println(n2);
int n3 = data3.toInt();
Serial.println(n3);
int n4 = data4.toInt();
Serial.println(n4);
int n5 = data5.toInt();
Serial.println(n5);

data1="";
data2="";
data3="";
data4="";
data5="";
}
}
``````

There are better ways, and `String` is not one of them. Seriously, **Don’t use `String`**™. Because you’re using a couple of libraries, especially the fairly large Ethernet lib, `String` will almost certainly cause problems sooner or later, as most people know.

PaulS’s suggested `atoi` and `strtok` functions could definitely be used, but since you’re just starting with this, I’d like to describe it in a way that will help you understand how those functions work, under-the-hood.

What others have been trying to guide you toward is realizing that the packet array contains ASCII values. (<–That’s a link you should check out sometime.) For example, if the packetBuffer array contains “1234,5678,9101,1121,3141,5161”, then

``````     packetBuffer[0] = '1';
``````

That is not the same as

``````     packetBuffer[0] = 1;
``````

The ASCII value of the ‘1’ character is actually 49, so if you want to convert one of those characters to a numeric value, you have to subtract 48, the ASCII value for ‘0’:

``````     oneDigit = packetBuffer[0] - 48;  // or...
oneDigit = packetBuffer[0] - '0';
``````

In your case, you want to convert 4 ASCII characters into 1 number. Each character is one digit.

For example, if you had the digit 1 and the digit 2, how would you combine them to make the number 12? Multiply the 1 by 10 and add 2. You have to do the same thing for your four digits:

``````    digit1=packetBuffer[0] - '0';
digit2=packetBuffer[1] - '0';
digit3=packetBuffer[2] - '0';
digit4=packetBuffer[3] - '0';
number = 1000*digit1 + 100*digit2 + 10*digit3 + digit4;
``````

With this basic approach, you could cut and paste this 6 times to get your 6 numbers from the ASCII packetBuffer. But there are better ways. This could work to extract the first number:

``````    int number = 0;
int i=0;
while ( isdigit( packetBuffer[i] ) ) {
number = (number*10) + (packetBuffer[i] - '0');
i++;
}
``````

`isdigit` is a built-in function that returns `true` when the ith character of the buffer is in the range ‘0’…‘9’. And notice how the current number value is multiplied by ten when another digit is added. At the end of this loop, `number` will be one thousand, two hundred and thirty-four: 1234. (This is very similar to what `atoi` does.)

You also need to skip the separating comma:

``````    while (!isdigit( packetBuffer[i] ))
i++; // skip character
``````

The ‘,’ character is not a digit, so that code increments `i` past the comma (and other non-digits, like spaces).

And you also need to get 5 more numbers. So let’s throw all that inside another loop and make an array for the numbers:

``````    int i=0;
int number[6]; // An array for the 6 numbers

//  Get all the numbers from the packet
for (int j = 0; j<6; j++) {
number[j] = 0;

//  Parse out one number
while ( isdigit( packetBuffer[i] ) ) {
number[j] = (number[j]*10) + (packetBuffer[i] - '0');
i++;
}

//  Skip comma (and other non-digits)
while (!isdigit( packetBuffer[i] ))
i++; // skip

}
``````

(This is very similar to what `strtok` does, except it doesn’t do the `atoi` part.)

BTW, arrays of `char`, like your `packetBuffer`, are also called “C strings”, and it’s easy to confuse them with the naughty `String`. The convention here has been to use lower-case “string” when referring to `char` array, and to use upper case when referring to `String`.

Another property of C strings is that they are terminated with a 0 value (not ‘0’!) to indicate there are no more characters in the array. Otherwise, how would you know? The `Serial.println` function watches for that special 0 character (a NUL terminator) to determine how much should be printed from the array. It appears that `UDP.read` returns a NUL-terminated packet, but it doesn’t hurt to be sure. Just add a few extra tests to make sure we don’t run off the end of the packet into random RAM

``````    int i=0;
int number[6]; // An array for the 6 numbers
for (int j = 0; j<6; j++) {
number[j] = 0;

while ( (i < packetSize) && isdigit( packetBuffer[i] ) ) {
number[j] = (number[j]*10) + (packetBuffer[i] - '0');
i++;
}

//  Skip comma (and other non-digits)
while ( (i < packetSize) && !isdigit( packetBuffer[i] ))
i++; // skip

}
``````

That’s it. Put all that inside the place where you have received a packet:

``````  if (packetSize) {
Serial.println(packetBuffer);

--> insert code here <--
}
``````

And please, I hope you notice the code is nicely formatted with indentation, and it shows up inside a white code box because I put code tags around it when I typed in this post, like this:

``````[code]  // Here is some code
if (packetSize) {
etc.
[/code]
``````

That turns into this when you post it:

``````  // Here is some code
if (packetSize) {
etc.
``````

Cheers,
/dev