How to catch using a variable the decimal value produced by lcd.print(0xFF, 10)

The function lcd.print(0xFF, 10) performs BINary-to-BCD conversion and shows the value (255) on the LCD. Is there any way to catch this BCD (decimal) value using a variable so that we can use it for some other purposes?

No.

You could use itoa() or one of its companions.

GolamMostafa:
The function lcd.print(0xFF, 10) performs BINary-to-BCD conversion

No, it does not. It prints the value in decimal notation.

It does NOT BCD it... It just prints it as decimal. Because a 0xF in BCD is undefined.

If you want the value of 255 then surprise! You already have that. Decimal or hex is just a representation but both 255 and 0xFF refer to the same number.

If you want the string with ASCII characters '2', '5' and '5' in it then use itoa(). Don't forget to add a null character if you want to use it as a string!

  1. Let us agree that Hexadecimal System is a compact form of Binary System. So, hexadecimal and
    binary words are interchangeable vocabularies! Similarly, to a human being it is decimal 3; the same
    thing appears to the machine as BCD 0011. Therefore, Decimal and BCD are interchangeable
    vocabularies!

  2. After the execution of lcd.print(0xABCDEF, 10) function, the display shows: 11259375; it is the
    correct decimal (BCD) value of ABCDEFh.

  3. I am convinced that the lcd.print() function internally performs the following BINary-to-BCD
    conversion algorithm; it is known as Horner Rule (of the year 1878). Mr. Horner derived this rule from
    Newton's polynomial (of the year 1778) : A(x) = anxn + an-1xn-1 + ........+ a0x0.

Horner Rule for converting n-bit BINary number into equivalent BCD:

BIN = bn-1bn-2........................b1b0
BCD = bn-12n-1 + bn-22n-2 + ....................+ b121 + b020
BCD = ((((......((IPBCD
2 + bn-1)2 + bn-2)2+..............+b)2+b0
Where: IPBCD = Initial Partial BCD

Computation Formula:
for (x = n-1; n>=0; n--)
IPBCD * 2 + bx -----> IPBCD ; IPBCD = Initial Partial BCD = 0000; bx = xth bit of the input BIN

Hope that the Software Engineer who wrote the function lcd.print() will clarify the case!!

Forget about the BCD stuff that you do not understand.

You were shown how to solve your problem.

GolamMostafa:
So, hexadecimal and binary words are interchangeable vocabularies!

Correct

GolamMostafa:
Therefore, Decimal and BCD are interchangeable vocabularies!

Also kind of correct but the compiler has no notion of BCD. BCD here is the weird one and has nothing to do with this all. For the compiler it doesn't matter is you write 255, 0xFF, 0377 or 0b11111111, all results in the same value in the register.

GolamMostafa:
the display shows: 11259375; it is the correct decimal (BCD) value of ABCDEFh.

Again, nothing to do with BCD. 0xABCDEF is NOT valid BCD. 0xABCDEF and 11259375 are just other notations for the same thing. BUT, the print doesn't send a varaible 11259375. It send the ASCII string "11259375" aka '1', '1', '2', '5', '9', '3', '7', '5'

GolamMostafa:
I am convinced that the lcd.print() function internally performs the following BINary-to-BCD

Sorry, that's just plain wrong. It has NOTHING to do with BCD. It more looks like (but not exactly)

unsigned long val = 11259375;

for(unsigned long i = 10000000; i; i /= 10){
  Serial.write(val / i + '0');
}

So again, what do you want to do with the value?

septillion:
It does NOT BCD it... It just prints it as decimal. Because a 0xF in BCD is undefined.

If you want the value of 255 then surprise! You already have that. Decimal or hex is just a representation but both 255 and 0xFF refer to the same number.

If you want the string with ASCII characters '2', '5' and '5' in it then use itoa(). Don't forget to add a null character if you want to use it as a string!

I wish to assign the value to a variable so that I can use it for some other purpose! Seeing the value on the output display device is not enough for me!

Did you bother to read about itoa() ?

Whandall:
Forget about the BCD stuff that you do not understand.

You were shown how to solve your problem.

Do you think that the lcd.print(0xABCDEF, 10) finds the corresponding decimal value (so large) just as (without any processing of the input Binary number) notation transformation? You can't overlook the inquiry of the serious technical matter by saying "you do not understand." There is nothing as such personal in a "Forum."

Pleaso do not blame me for your misunderstandings/misbelieves,
I was in no way connected to your education.

septillion:
Correct
Also kind of correct but the compiler has no notion of BCD. BCD here is the weird one and has nothing to do with this all. For the compiler it doesn't matter is you write 255, 0xFF, 0377 or 0b11111111, all results in the same value in the register.
Again, nothing to do with BCD. 0xABCDEF is NOT valid BCD. 0xABCDEF and 11259375 are just other notations for the same thing. BUT, the print doesn't send a varaible 11259375. It send the ASCII string "11259375" aka '1', '1', '2', '5', '9', '3', '7', '5'
Sorry, that's just plain wrong. It has NOTHING to do with BCD. It more looks like (but not exactly)

unsigned long val = 11259375;

for(unsigned long i = 10000000; i; i /= 10){
 Serial.write(val / i + '0');
}




So again, what do you want to do with the value?

I wish to store it in a variable for subsequent use by sending it to a remote controller.

Whandall:
Did you bother to read about itoa() ?

The prototype of lcd.print() says that the function returns 'number of bytes' printed (shown) in the LCD and not the converted value. We wish to receive the value in order to save it in a variable for subsequent usage.

Whandall:
No.

You could use itoa() or one of its companions.
No, it does not. It prints the value in decimal notation.

How does the function lcd.print() know that the decimal notation of 0xFFFF is 65535 without processing the input Binary (Hex) number? (F means 15, let us print 5; then ......!)

Whandall:
Pleaso do not blame me for your misunderstandings/misbelieves,
I was in no way connected to your education.

This is the case of finding out how the lcd.print(0xFFFF, 10) finds and then prints the decimal value.

  1. The function must first find the decimal value.
  2. The function will then find the ASCII codes for the digits of the decimal (BCD) number.
  3. The function will then send the series of ASCII codes on the Text-type LCD.
  4. The Forum aims at learning from each other and not to demonstrate who knows more!

GolamMostafa:
I wish to assign the value to a variable so that I can use it for some other purpose!

Then TADAAAA, you are done!

No matter if you write it as hex or as a decimal, the variable holds THE SAME value!

Do you get confused when you read to get Three milk? Or 3 milk? Or III milk?

A simple test:

void setup(){
  Serial.begin(115200);
  
  byte asHex = 0xFF;
  byte asDec = 255;
  
  Serial.println(F("Let's just print them!"));
  Serial.println(asHex);
  Serial.println(asDec);
  Serial.println(F("Print them as hex"));
  Serial.println(asHex, HEX);
  Serial.println(asDec, HEX);
  Serial.println(F("Print them as binary"));
  Serial.println(asHex, BIN);
  Serial.println(asDec, BIN);
  Serial.println(F("Or even as damn octal"));
  Serial.println(asHex, OCT);
  Serial.println(asDec, OCT);

  Serial.println(F("Both variables are 100% the same!"));
  Serial.println(F("It's just notation"));
}

void loop(){
  
}

GolamMostafa:
4. The Forum aims at learning from each other and not to demonstrate who knows more!

True, so why do you think you know it better?

GolamMostafa:
How does the function lcd.print() know that the decimal notation of 0xFFFF is 65535 without processing the input Binary (Hex) number? (F means 15, let us print 5; then …!)

By dividing the number by the base repeatedly and converting the remainders to digits.

Have a look at the code

size_t Print::printNumber(unsigned long n, uint8_t base)
{
  char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
  char *str = &buf[sizeof(buf) - 1];

  *str = '\0';

  // prevent crash if called with base == 1
  if (base < 2) base = 10;

  do {
    char c = n % base;
    n /= base;

    *--str = c < 10 ? c + '0' : c + 'A' - 10;
  } while(n);

  return write(str);
}

But that only holds if he want it as a string. He doesn't need that if he just want it as a "decimal" value. (In quotes because there is no such thing as a decimal value. Only a decimal representation of a value.)

Whandall:
By dividing the number by the base repeatedly and converting the remainders to digits.

Have a look at the code

size_t Print::printNumber(unsigned long n, uint8_t base)

{
  char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
  char *str = &buf[sizeof(buf) - 1];

*str = ‘\0’;

// prevent crash if called with base == 1
  if (base < 2) base = 10;

do {
    char c = n % base;
    n /= base;

*–str = c < 10 ? c + ‘0’ : c + ‘A’ - 10;
  } while(n);

return write(str);
}

My query was very simple! I just wanted to know if there exits any kind of means for catching the ‘decimal representation (65535)’ in data form that the function lcd.print(0xFFFF, 10) produces for the number (0xFFFF). Let me tell about my intended purpose for knowing it.

  1. Take the example of a Digital Weighing Machine.

  2. Input weight is in decimal format (say, 27 kg). The Load Cell passes DC voltage (Vd) to the ADC. The
    MCU reads hex value (Vh) from the output of ADC. The Vh is conditioned by the gain and offset of the
    input device (the signal goes through calibration) and the MCU finds weight in Hex Format. In order to
    show the magnitude of the weight to the customer, we must convert it into decimal format. This binary
    to BCD (not decimal) conversion is conveniently done using Horner Rule. The Horner Rule can be coded
    in Machine Code or Assembly Code or C Code.

  3. Using Arduino C, we may write the Horner Rule as:
    unsigned int x = 0x1FFF ; //(say BINary weight)
    unsigned int bcdwt = 0x00;
    for (x=15; x>=0; x–)
    {
    bcdwt = bcdwt * 2 + bx; // bx = bit-15, bit-14, … of the input Binary weight
    bcdadj(); // to adjust incorrect BCD into correct BCD [09+01 = 0A —> 10]
    }

  4. While learning LCD programming using Arduino IDE, I have come to know the amazing power of
    the lcd.print (0x1FFF, 10) function which immediately shows me the BCD image (8191) on the display.

  5. I have immediately thought that if I could somehow catch the decimal (BCD) value in data form of the
    lcd.print() function, I could easily avoid writing the C codes of Step-3.

  6. It has been very encouraging to see the involvement of so many persons in this issue.

  7. Thanks to all.

You are free to tell any story here,
but do yourself a favor and study the internal representation of digital data
and the differences between internal and external representation, notably for integers.
One such internal representation is described here: Binary coded decimal (BCD)

Printing the decimal value of an integer is not connected to BCD.

Decimal is NOT the same as BCD. BCD uses four binary bits to code a decimal digit and packs two of those into a byte. Decimal uses base ten digits 0 to 9.

OP. If I write:

int x = 255;
int y = 0xFF;

Do you think that somehow x and y are storing different data? I assure you, in any processor it is all stored as binary. It doesn't matter how you wrote it in the code it has the same value and is stored as 0b11111111. There is no "conversion" needed or even possible.