Please post your full sketch, using code tags when you do
Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination
I did. Whoever wrote this must really feel that 20th century glow. Compact, efficient, obscure and clever. How they once rolled. Maybe, maybe, they had to. I don't think so.
Far too clever. An abomination. You are fired.
for (i = 0; 0 != dec && i < size; i++, dec /= 10)
arr [i] = dec % 10;
Yes it works. But it should be written in plain code. These days, the plain code will be not be significantly different in terms of space or time.
But it will be something normal ppl can read, and see as correct, rather than something that might appear as a puzzle-time diversion in a technical journal.
int
decToBcd (
unsigned dec,
byte *arr,
int size )
{
int i;
i = 0;
repeat:
if (0 == dec)
goto done;
arr [i] = dec % 10;
i = i + 1;
if (i >= size)
goto done;
dec = dec / 10;
goto repeat;
done:
return i;
}
Thanks for making my point. I will not think any more about it. Life is too short.
Your algorithm's need for initialization, a condition to test, and a means of moving along does not justify shoehorning it into a for loop.
Here's a plain version of the algorithm using while - yes, tested in your sketch:
int decToBcd(unsigned dec, byte *arr, int size )
{
int i = 0;
while (dec != 0 && i < size) {
arr[i] = dec % 10;
dec /= 10;
i++;
}
return i;
}
I appreciate compact code when it enhances clarity, but in this case, the for loop adds more cleverness than it does readability. While it works and fits the mold of init/test/update, it’s dense enough that most readers will need to pause and mentally unpack it. Something we ideally avoid in everyday code.
Rewriting it as a simple while loop improves readability. It makes the logical steps immediately apparent, which helps future readers (or reviewers (or ourselves)) focus on the intent rather than deciphering the structure.
And should an error be discovered in the function, fixing it would be easier in the naive version. I wonder how long it took whoever wrote that to get it right. Unless she’s in the habit of doing things like this, that’s more wasted time.
Going for clarity is rarely wasted effort—especially in code we share.
bcd: 2 digits per byte, add '0' to a nybble and get text.
Arduino integer types store signed or unsigned binary integers in 8, 16, 32 and 64 bit widths. If you want to break those down quickly to text; use hexadecimal for digits 0-9, + '0' / for digits A-F, -10 + 'A'.
The chip runs binary operations. Decimal takes time to convert to print, it is only for display, the value is what is stored and printed.. just.. how. Years ago there was a long thread that gave a very fast divide by 5 that led to a super div/10, it was in the Playground, Rob Tillart was instrumental. The div/5... how did that work?
Levels of understanding separate understanding from separating-eyes. I like the all-levels solution that can be made tight (as I learn), because; I once posted a button interrupt that would work in a user's blocking code, yet was quickly shown the light, being asked why I would post this advanced techniques for a learning coder. I agreed that I was wrong. Before I agreed, I had thought, "If I can learn this, so can, probably, everyone," but the dress-down was not about the widget that worked, rather, that this widget only worked "here" and the user would not know where else, but more dangerously, let the user continue to write poor code. That made lots of sense to me. I still like both camps.
More briefly perhaps, but not in the spirt of strong typing. && is a logical comparison between bool types. i<size is a bool type. 0 != dec is a bool type. dec is not.
"Eagleson's Law: Any code of your own that you haven't looked at for six or more months might as well have been written by someone else." - Alan Eagleson
But it would be implicitly converted, similar to when arithmetic is performed using inconsistent types (e.g., 2*3.14159); is it more important to follow the "spirit of strong typing" in the case of boolean operators (as opposed to arithmetic operators)?
i'm not trying to be clever. it's simply a different perspective amoung the many repsonses the OP has to read. either it helps or it doesn't, how can it hurt?
i saw two stopping conditions and 2 simple loop adjustments during each iteration which fit the parameters of a for statement and a conversion from a single decimal digit to bcd.
i don't know how "new" to assume the OP is. I don't know how far down the rabbit hole of simplification to go (e.g. is a for statement too complicated)? and it seems someone will always suggest
it is still not simple enough,
that the variable names are too short,
not descriptive enough,
not what they would have used,
not enough comments or
it could have been done another way (e.g. '(dec)vs(0 == dec)`).
i think less code and short variable names have less unnecessary stuff (i.e. noise) and is easier to focus on
i don't know if the OP is high school, scientist, ex COBOL programmer or a retired guy in their 70s.
i don't know that they even care how the code works, or even care to learn to program, that they just want their project to work.
i assume the OP can ask questions. some have and i've been blind sided with questions i hadn't imagined.