Displaying data problem.

Hey.
I have such a problem.
I need a value e.g. 1234 to divide into 2 elements

1 term (A) = (digit 3 and 4) = 34
2 term (B) = (digit 1 and 2) = 12

in addition, each digit must be as uint8_t

I did something like that

long integer = 1234;
uint8_t digits[7]; //7 na wypadek większej liczby
for(int i = 0; i < 7; i++){
     digits[i] = integer % 10; 
     integer /= 10;    
Serial.print(digits[i]);

On the serial print I get the number 4321000

Now I would like to paste individual numbers into the program

A = (digits[1], digits[0]);
B = (digits[3], digits[2]);

Serial.print("A-"); Serial.println(A);
Serial.print("B-"); Serial.Println(B);

At output A, I get the number 4 instead of the number 34
At output B I get the number 2 instead of the number 12

I have to combine it and I can’t use multiplication or division, otherwise the data in the program does not work properly
How to solve it ??

A = integer % 100;
B = integer  / 100;

Additionally this syntax...

A = (digits[1], digits[0]);
B = (digits[3], digits[2]);

...is nonsense. It compiles, but I've no idea what it does. I suspect just returns the first value and throws the remaining terms away.

Damian01:
I have to combine it and I can't use multiplication or division, otherwise the data in the program does not work properly

Would help if you could post the entire sketch, because that statement does not make sense out of context.

Look up the comma operator. You won't get far with coding, if your approach is to just make stuff up.

A = (digits[1], digits[0]);

What does any of this mean?

I have to combine it and I can't use multiplication or division, otherwise the data in the program does not work properly

Are you longing to see your given number (1234) to be separated as shown in Fig-1?

smQW.png
Figure-1:

smQW.png

If you know how to get 3 and 4 from 34, it should be painfully obvious how to get 34 from 3 and 4.

I can do this:

a = ((digits[1]*10) + digits[0]);
b = ((digits[3]*10) + digits[2]);

hen I will receive
A = 34
B = 12

But the device to which I send data will display it incorrectly.
I need to send the number 3 separately and 4 separately
similarly
separately 2 separately 1 and each digit must be in uint8_t

That's why I have no idea how to solve this problem.

What device?

aarg:
What device?

If it's important :slight_smile:

Spektrum ix12

Damian01:
If it's important :slight_smile:

Spektrum ix12

Sometimes I want to go look up a device protocol when a poster doesn't explain any details about it.

aarg:
Sometimes I want to go look up a device protocol when a poster doesn't explain any details about it.

From the document I have only this information:

UINT16 altitudeLow; // BCD, meters, format 3.1 (Low bits of alt)

You are contradicting yourself. First you say that 1 and 2 have to be in the same byte, then when you're told how to do that, you say no, they have to be "separate"... everyone must be as confused as I am...

aarg:
You are contradicting yourself. First you say that 1 and 2 have to be in the same byte, then when you're told how to do that, you say no, they have to be "separate"... everyone must be as confused as I am...

You're right. I explained the problem wrong at the beginning

Damian01:
From the document I have only this information:

UINT16 altitudeLow; // BCD, meters, format 3.1 (Low bits of alt)

Take this as two separate problems:

  1. convert an integer value into a BCD value stored in a uint16_t
  2. split the uint16_t result into two bytes

How and where do you want to "display" them? Your BCD value is not in ASCII format.

UINT16 altitudeLow; // BCD, meters, format 3.1 (Low bits of alt)

If you need BCD (binary coded decimal) then try:

a = ((digits[1]*16) + digits[0]);
b = ((digits[3]*16) + digits[2]);

//use Serial.write to send non-ascii data
Serial.write(a);
Serial.write(b);

Or

//use Serial.write to send non-ascii data
Serial.write(b);
Serial.write(a);

depending on your target system's endian-ness.

david_2018:
If you need BCD (binary coded decimal) then try:

a = ((digits[1]*16) + digits[0]);

b = ((digits[3]*16) + digits[2]);

//use Serial.write to send non-ascii data
Serial.write(a);
Serial.write(b);

I use a BCD converter like this

uint8_t bcd(uint8_t num){
  uint8_t ten=num/10;
  return (ten<<4)+(num-ten*10);
}

I added your suggestion to the program and it's better but not perfect

Shows ok above 10m
but below 10 it displays something like this

0.2.jpg0.9.jpg5.6.jpg

0.2.jpg

0.9.jpg

5.6.jpg