How to divide a variable into two variables

Good evening everyone,
I introduce myself, I am an electronic expert with a passion for electronics and programming.
I had known arduino for a long time but only now have I approached this environment.

Since searching on google and on various forums I was unable to understand
how to divide a variable into several variables.

For example:

int x; // Declaration of local variables
x = 10; // Starting Variable

I need to be able to divide the above variable into two variables.

y = 1; // First number of variable
z = 0; // Second number of variable

In the variable Y I need to store the ten, and in the variable Z I need to store the unit.

I could not find anything concrete, can you kindly help me?

Many thanks in advance

Do you mean you need to SEPARATE the digits? Remember all numeric variables are binary, not base 10. So you need to convert the numeric into something like a string and then pick out the base 10 digits.

Paul

Or you can use division (/) and modulus (%) operators.
Beware of the modulus operator on negative numbers.

sounds like you want to translate a number into ascii digits

if so, sprintf() is typically used to create character strings to print the value of variables using ascii characters

    char s [20];
    sprintf (s, "%d", x);
    Serial.println (s);

Paul_KD7HB:
Do you mean you need to SEPARATE the digits? Remember all numeric variables are binary, not base 10. So you need to convert the numeric into something like a string and then pick out the base 10 digits.

Paul

TheMemberFormerlyKnownAsAWOL:
Or you can use division (/) and modulus (%) operators.
Beware of the modulus operator on negative numbers.

Thank you very much for the fast reply !

Paul, I don't care about the base 10 digits, I just need the first " number " and the " second " number.
Have you some examples for me ?

@TheMember...AWOL, Anyway can you kindly provide me some example with division operator,
for understand what do you mean ? Thank you !

I'm not a professional C++ programmer, with some examples I can understand, sorry.

Thank you very much in advance !

gcjr:
sounds like you want to translate a number into ascii digits

if so, sprintf() is typically used to create character strings to print the value of variables using ascii characters

    char s [20];

sprintf (s, "%d", x);
    Serial.println (s);

I'll check more info about "sprintf" thank you very much.

I had checked but it seems to be useful for include different variables in one.
I need the opposite solution, split one variable in two different vars.

Thank you !

I don't care about the base 10 digits

I think you do.

The decimal number ten is represented in the computer as 00001010 (8 bits binary).
It can also be represented in hexadecimal as A.

But I might be wrong, so what do you actually want to do?

Here is an example of breaking up a 32 bit int into 4 bytes:

  if (!PassTwo)
        {
          rx_frame.FIR.B.FF = CAN_frame_std;
          rx_frame.MsgID = 1;
          rx_frame.FIR.B.DLC = 8;
          rx_frame.data.u8[0] = *item & 0xFF;
          rx_frame.data.u8[1] = (*item >> 8) & 0xFF;
          rx_frame.data.u8[2] = (*item >> 16) & 0xFF;
          rx_frame.data.u8[3] = (*item >> 24) & 0xFF;
          PassTwo = true;
        } else {
          rx_frame.data.u8[4] = *item & 0xFF;;
          rx_frame.data.u8[5] = (*item >> 8) & 0xFF;
          rx_frame.data.u8[6] = (*item >> 16) & 0xFF;
          rx_frame.data.u8[7] = (*item >> 24) & 0xFF;
          ESP32Can.CANWriteFrame(&rx_frame); // send items over CAN buss
          PassTwo = false;
        }
[code] that may or may not help.

jremington thank you for replying ! :slight_smile:

I don't care about the decimal number because the first digit is not alway number "1",
but it could be "0","1","2","3","4". So I can't use hex "A" or "00001010" (8 bits binary) as you told.

I just need to split the var in two, because I've to print each var on a 8 segment display
the own value using a multiplexing system.

Thank you ! :slight_smile:

Idahowalker:
Here is an example of breaking up a 32 bit int into 4 bytes:

  if (!PassTwo)

{
          rx_frame.FIR.B.FF = CAN_frame_std;
          rx_frame.MsgID = 1;
          rx_frame.FIR.B.DLC = 8;
          rx_frame.data.u8[0] = *item & 0xFF;
          rx_frame.data.u8[1] = (*item >> 8) & 0xFF;
          rx_frame.data.u8[2] = (*item >> 16) & 0xFF;
          rx_frame.data.u8[3] = (*item >> 24) & 0xFF;
          PassTwo = true;
        } else {
          rx_frame.data.u8[4] = *item & 0xFF;;
          rx_frame.data.u8[5] = (*item >> 8) & 0xFF;
          rx_frame.data.u8[6] = (*item >> 16) & 0xFF;
          rx_frame.data.u8[7] = (*item >> 24) & 0xFF;
          ESP32Can.CANWriteFrame(&rx_frame); // send items over CAN buss
          PassTwo = false;
        }

[code] that may or may not help.

Idahowalker, please can you explain how this can help me ? I really don't understand how can I use it.
Thank you !

I just need to split the var in two

Explain what "split in two" means, with some more examples.

the first digit is not alway number "1", but it could be "0",

For example, please split "1" into two variables.

The variable can take values from 0 to 50.

EG.

if the var X = 10, I need Y = 1 and Z = 0
if the var X = 15, I need Y = 1 and Z = 5
if the var X = 17, I need Y = 1 and Z = 7
if the var X = 22, I need Y = 2 and Z = 2
if the var X = 25, I need Y = 2 and Z = 5
if the var X = 28, I need Y = 2 and Z = 8

I hope I have expressed myself clearly :slight_smile:

For God’s sake, this is primary school arithmetic.

TheMemberFormerlyKnownAsAWOL:
For God’s sake, this is primary school arithmetic.

Sorry dear, are you just writing to increase your post count?
If you don’t have a solution or a suggestion, why don’t you leave this topic?

What do you mean by this answer … ?
If it is so obvious why don’t you give a solution instead of complaining?

In elementary school arithmetic nobody taught me to divide a variable consisting of two integers into two new variables. Especially in the C ++ programming language.
Where did you attend the elementary school? At Oxford University?

Did you read reply #2, or are you just trying to increase your post count?

If you're so brilliant, why don't you explain to me how to get
the following operation using the (/) division operator ?

X = 27; --> Y = 2; and Z = 7;
or
X = 13; --> Y = 1; and Z = 3;
or
X = 32; --> Y = 3; and Z = 2;
...

Let me know,
Thank you dear

Re-read reply #2; it mentions another operator.

TheMemberFormerlyKnownAsAWOL:
Re-read reply #2; it mentions another operator.

Do you mean about this reply ? :

@TheMember…AWOL, Anyway can you kindly provide me some example with division operator,
for understand what do you mean ? Thank you !

Because if you refer to this reply, I think probably it was too hard to understand for you that I was asking, i just meant kindly of gave me some examples for understand the logic about the operation required.

After 3 useless message you still have not wrote an interesting message about the problem. Please if you’ve to reply with another useless message, don’t reply. Thank you very much

Are you aware that there is a language reference?

Reply #2

Do you intend expending any effort researching your problem, or do you want to be spoon-fed?

I’ve known and worked with many electronics experts; some claimed to not be able to program, but not one of them couldn’t do arithmetic.

i just meant kindly of gave me some examples for understand the logic about the operation required.

This is decimal (base 10) arithmetic, as taught in primary schools around the world. The technique was discovered a couple of thousand years ago and has proved to be very useful.

I think you will also find it useful, for example, $15 can be broken down into one paper bill or coin worth $10, and five paper bills or coins, each worth $1.

If you are not familiar with the operations, your children can probably explain the details.

@TheMemberFormerlyKnownAsAWOL I learned divison in the realm of integers below 100
in the second class (at an age of 8 ) in a pretty basic German primary school.