Arduino, how to grab the first 3 MSDs of an unsigned long integer

ApacheKid

Joined Jan 12, 2015
1,619
I wouldn't say performance is tight. I just want a solution that gives me the end result that I'm looking for. No more, no less.
Doesn't seem like this should be a big deal.
So if you recommend avoiding string conversions and sprintf, then how would you do it?
Exactly what's described here:

1642002794795.png

Coding that in C should be pretty easy, the C# differs of course but only marginally.

If you need it in C I can probably do that if you'd like.
 

ApacheKid

Joined Jan 12, 2015
1,619
Here's the C:

Code:
void ConvertUlongToBCD(unsigned long Value, char * Digits)
{
    for (int I = 0; I < 20; I++)
    {
        Digits[I] = Value % 10;
        Value = Value / 10;
    }
}
Unlike C# preinitialized data isn't the case in C so the check and break has to go.

Here's how you'd call it:
Code:
    char digits[20];

    ConvertUlongToBCD(120440765, digits);
That's it, problem solved, just take what you need from the 20 char array and you're good.

1642003611659.png

If you want too, you can build the array in the other direction, then elements 0, 1, 2 become the leading digits :

1642003818477.png
 
Last edited:

Thread Starter

joblake326

Joined Jan 9, 2022
21
{
for (int I = 0; I < 20; I++)
{
Digits = Value % 10;
Value = Value / 10;
}
}
Here's how you'd call it:
char digits[20];
ConvertUlongToBCD(120440765, digits);
That's it, problem solved, just take what you need from the 20 char array and you're good.

Ok. Let me ask a dumb question: (bare with me since I obviously don't know what I'm doing)
Why can't I just take myUnsignedLong and do this:
myUnsignedLong = myUnsignedLong/1000
 

ApacheKid

Joined Jan 12, 2015
1,619
You can if that is specifically and always what you want. That step basically shifts the decimal to the right and loses the three least significant digits. If that is what you want then you can do that.

But if you want the first N digits it won't work.
 

ApacheKid

Joined Jan 12, 2015
1,619
That's all I need for now anyway.
Thanks for all your help (and patience).
NP, was a bit bored anyway.

But note you did write:

"I have an unsigned long variable with an incrementing big number in it but I only care about the first 3 or 4 digits"

That's an example of poor problem definition, a cause of so many woes in this business, perhaps what you should have said was:

"I have an unsigned long variable with an incrementing big number in it but I don't care about the last 3 or 4 digits".

From the outset, they way you stated the problem, the beginning of the number, the first digit and beyond, was the focus, the reader focused on that.

Just FYI.
 

Ya’akov

Joined Jan 27, 2019
9,170
NP, was a bit bored anyway.

But note you did write:

"I have an unsigned long variable with an incrementing big number in it but I only care about the first 3 or 4 digits"

That's an example of poor problem definition, a cause of so many woes in this business, perhaps what you should have said was:

"I have an unsigned long variable with an incrementing big number in it but I don't care about the last 3 or 4 digits".

From the outset, they way you stated the problem, the beginning of the number, the first digit and beyond, was the focus, the reader focused on that.

Just FYI.
I would be inclined to say something like "Have an unsigned long and want to display the N most significant digits on an LCD display." Primarily because I (wouldn't) know if there was something involving he displaying itself that could simplify it.

That is to say, getting the N most significant digits might not be the best solution when displaying them.
 

Thread Starter

joblake326

Joined Jan 9, 2022
21
NP, was a bit bored anyway.

But note you did write:

"I have an unsigned long variable with an incrementing big number in it but I only care about the first 3 or 4 digits"

That's an example of poor problem definition, a cause of so many woes in this business, perhaps what you should have said was:

"I have an unsigned long variable with an incrementing big number in it but I don't care about the last 3 or 4 digits".

From the outset, they way you stated the problem, the beginning of the number, the first digit and beyond, was the focus, the reader focused on that.

Just FYI.
Point taken. Thanks again.
I'll be back...
 
Top