nickgammon / BigNumber

BigNumber library for the Arduino
MIT License
85 stars 22 forks source link

Serial.write #17

Closed Amine1079 closed 3 years ago

Amine1079 commented 3 years ago

hello, how to send a big number with serial in Arduino ? I have tried to send with serial.write but the result is false and thanks

nickgammon commented 3 years ago

You would need to convert it to a string. For example, from the page Arbitrary precision (big number) library port for Arduino:

// function to display a big number and free it afterwards
void printBignum (BigNumber n)
{
  char * s = n.toString ();
  Serial.println (s);
  free (s);
}  // end of printBignum
Amine1079 commented 3 years ago

I'm trying to send a big number from master(Arduino) to slave(Arduino) I have tried with printBignum but the same problem Capture

nickgammon commented 3 years ago

Serial.read reads one byte. You need to make your slave read multiple bytes, for example until you hit a newline. On your slave you received 52, 49, 56, 53 ... Now if you look at an ASCII code chart, that is equivalent to '4', '1', '8', '5' which is what you are sending. Once you have received the whole number (24 bytes in your case by the looks) you can use the Bignum library to convert that string back into a big number.

nickgammon commented 3 years ago

There are some tips about reading serial data until you get a newline or similar on my page about processing serial data.

Amine1079 commented 3 years ago

Thank you :)

Amine1079 commented 3 years ago

I have tried to convert String to BigNumber but : error: no match for 'operator=' (operand types are 'BigNumber' and 'String') 1

nickgammon commented 3 years ago

See https://github.com/nickgammon/BigNumber

create your BigNumber

  BigNumber bigNum;
  bigNum = "67411411944031530524562029520200239450929984281572279077458355238873731477537";

You supply a string (not a String) to the BigNumber. The link I gave above about processing serial input shows how to do that. If you use the String class you are likely to run out of memory because of memory fragmentation.

Make an array of char, and put the incoming data into it. In your case an array of (say) 33 bytes should be enough to hold 32 bytes of digits, plus a terminating 0x00 byte.