# Mathematical Conundrum

**URL:** <https://boards.straightdope.com/t/mathematical-conundrum/237628>\
**Category:** Miscellaneous and Personal Stuff I Must Share\
**Created:** [March 31, 2004, 11:14pm UTC](https://boards.straightdope.com/t/mathematical-conundrum/237628 "2004-03-31T23:14:14Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![StephenG](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@StephenG](https://boards.straightdope.com/u/StephenG)\
**Post date:** [March 31, 2004, 11:14pm UTC](https://boards.straightdope.com/t/mathematical-conundrum/237628/1 "2004-03-31T23:14:14Z")

</div>

This is a real world problem, to which I cannot guarantee an answer. (I think I have an answer, but I could be wrong.)

I need to convert a nine-digit number into an eight-digit alphanumeric code. My number can be anything from 000000000 to 999999999. Simply dropping one digit (the last, in this example) won’t do, because then, say, 123454678(9) would be indistinguishable from 12345679(9). I could change 123456781 into 12345678a, 123456782 into 12345678b, etc. – letters are perfectly allowed – but then I still have too many digits.

Any thoughts? Is this problem actually solvable? I think I have a solution, but it’s messy – it involves too many steps and options and possibilities.) However, I will post it later if people want. (I’m not describing it because I don’t want to swing people to my way of thinking. I don’t _like_ my answer.)

I may get dinner out of this, but I think my boss wants an answer from somebody (anybody!) soon.

Thanks!

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [March 31, 2004, 11:18pm UTC](https://boards.straightdope.com/t/mathematical-conundrum/237628/2 "2004-03-31T23:18:51Z")

</div>

Do you need to take up the full 8 characters? If not, you can just convert it to base 36, in which 10[sup]10[/sup] is only 7 digits long.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [March 31, 2004, 11:20pm UTC](https://boards.straightdope.com/t/mathematical-conundrum/237628/3 "2004-03-31T23:20:07Z")

</div>

…and 10[sup]9[/sup], the upper bound on the numbers you care about, is only 6 digits long.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [March 31, 2004, 11:23pm UTC](https://boards.straightdope.com/t/mathematical-conundrum/237628/4 "2004-03-31T23:23:11Z")

</div>

FWIW, any base greater than 13 will work.

---

<div class="post-metadata">

**Author:** ![Earthling](https://avatars.discourse-cdn.com/v4/letter/e/90db22/32.png) [@Earthling](https://boards.straightdope.com/u/Earthling)\
**Post date:** [March 31, 2004, 11:34pm UTC](https://boards.straightdope.com/t/mathematical-conundrum/237628/5 "2004-03-31T23:34:53Z")

</div>

Which means you can use hexadecimal notation (very common). Windows’s calculator (in scientific mode) can do the conversion for you. 999,999,999 = 3B9AC9FF

---

<div class="post-metadata">

**Author:** ![StephenG](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@StephenG](https://boards.straightdope.com/u/StephenG)\
**Post date:** [March 31, 2004, 11:40pm UTC](https://boards.straightdope.com/t/mathematical-conundrum/237628/6 "2004-03-31T23:40:31Z")

</div>

Oooh, good thinking – I don’t think I’d’ve ever thought of that. You’re right, there’s no reason to use the full 8 characters at all. And a conversion to hex isn’t hard at all (even for folk who don’t do math).

Thank you!

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [April 1, 2004, 12:16am UTC](https://boards.straightdope.com/t/mathematical-conundrum/237628/7 "2004-04-01T00:16:33Z")

</div>

Do you need to automate this somehow?

---

<div class="post-metadata">

**Author:** ![StephenG](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@StephenG](https://boards.straightdope.com/u/StephenG)\
**Post date:** [April 1, 2004, 4:06am UTC](https://boards.straightdope.com/t/mathematical-conundrum/237628/8 "2004-04-01T04:06:00Z")

</div>

Yes, it will be automated, I believe. But it should be pretty easy to program this, I think. I mean, surely Java (or C++ or whatever our tech team uses) has a function to convert decimal numbers to hex?

---

<div class="post-metadata">

**Author:** ![Earthling](https://avatars.discourse-cdn.com/v4/letter/e/90db22/32.png) [@Earthling](https://boards.straightdope.com/u/Earthling)\
**Post date:** [April 1, 2004, 4:30am UTC](https://boards.straightdope.com/t/mathematical-conundrum/237628/9 "2004-04-01T04:30:31Z")

</div>

I don’t know if your application is for numbers to be converted one at a time, or if you have a large list that can be copied and pasted. If it’s the latter, a simple non-programming approach would be to use Excel’s DEC2HEX function. Example:

Cell A1 value = 999999999  
Cell B1 formula =DEC2HEX(a1)  
Cell B1 result = 3B9AC9FF

You can then apply this formula to as many cells as needed.

---

<div class="post-metadata">

**Author:** ![StephenG](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@StephenG](https://boards.straightdope.com/u/StephenG)\
**Post date:** [April 1, 2004, 4:48am UTC](https://boards.straightdope.com/t/mathematical-conundrum/237628/10 "2004-04-01T04:48:25Z")

</div>

There’ll be a long list right at the beginning, and then new numbers trickling in over time. I definitely appreciate the Excel heads-up, though.

I’m not in charge of this project (thank God), I was just invited to take a stap at the 9 -\> 8 problem. Whenever we’ve had 9 characters before, it’s been 3 letters + 6 numbers, so one letter could be dropped, or nine digits where one or more could be dropped safely. This is a new challenge…

Hopefuly, my boss will like the hex solution. I think it’s ingenious myself… and it should be pretty easy to implement.

---

<div class="post-metadata">

**Author:** ![MissMossie](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@MissMossie](https://boards.straightdope.com/u/MissMossie)\
**Post date:** [April 1, 2004, 6:50am UTC](https://boards.straightdope.com/t/mathematical-conundrum/237628/11 "2004-04-01T06:50:51Z")

</div>

I know that Java has the ability to convert decimal integers to hexidecimal notation in the Integer class (included in java.lang). The method is defined as follows:

public static String toHexString(int num)

All you have to do is plug in an integer and it returns a string that has that integer in hexidecimal format. FYI, this same class has a toBinaryString and a toOctalString method that work much the same way except that they return binary and octal strings.

-Mosquito

---

<div class="post-metadata">

**Author:** ![StephenG](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@StephenG](https://boards.straightdope.com/u/StephenG)\
**Post date:** [April 3, 2004, 5:04am UTC](https://boards.straightdope.com/t/mathematical-conundrum/237628/12 "2004-04-03T05:04:32Z")

</div>

Thanks, Mosquito – I figured as much, though it’s been a few years since I studied any Java.

The good news is, I don’t have to implement these solutions – I’m just challenged to think of them. So far, no one else in my office has come up with anything and I have two – one thanks to you folks, and one ugly one that works. Life is good…

I’m curious – if we had to use all eight characters, is there another way to do it? What if we get a second client who uses nine distinct numbers, and we need to distinguish between the two clients’ accounts?

---

<div class="post-metadata">

**Author:** ![Earthling](https://avatars.discourse-cdn.com/v4/letter/e/90db22/32.png) [@Earthling](https://boards.straightdope.com/u/Earthling)\
**Post date:** [April 3, 2004, 8:07am UTC](https://boards.straightdope.com/t/mathematical-conundrum/237628/13 "2004-04-03T08:07:16Z")

</div>

If all you need to do is pad the digits so all 8 slots are used, this is a piece of cake.

In Excel, you just have to modify the DEC2HEX formula with an additional character to specify the number of digits needed – e.g., if the value in cell A1 is 15, then DEC2HEX(a1,8) will return 0000000F.

I don’t know Java, but I assume it should be equally simple to do something similar in any programming language. To give an example in awk:

```auto

echo "15" | awk '{printf("%08x
", $0)}'
Gives the result 0000000f

```

The %08x says to pad out 8 spaces with zeros in hex (x), and  
is the carriage-return character. $0 is standard input for awk. Similarly, in the bash command line,

```auto

printf '%08x
' 15
Returns 0000000f

```

If you need to distinguish between two clients who may give you duplicate 9-digit numbers, a crude way to accomplish this would be to convert them to a higher number base (so fewer characters result), apply a different checksum algorithm to each client’s data, and place that result in the 8th slot. I Googled on “base conversion calculator” and found a bunch of hits ([sample 1](http://www.swcp.com/~spsvs/resume/BaseConversion/BaseConversion.html), [sample 2](http://www.math.com/students/converters/source/base.htm)). It appears that base 36 needs only 6 spaces max. to convert a 9-digit decimal number. [This](http://www.bsdg.org/swag/MATH/0036.PAS.html) appears to be a little PASCAL program to convert from base 10 to 36 – I can’t vouch for its accuracy, though; it’s just the first Google hit.

---

<div class="post-metadata">

**Author:** ![Earthling](https://avatars.discourse-cdn.com/v4/letter/e/90db22/32.png) [@Earthling](https://boards.straightdope.com/u/Earthling)\
**Post date:** [April 3, 2004, 8:50am UTC](https://boards.straightdope.com/t/mathematical-conundrum/237628/14 "2004-04-03T08:50:37Z")

</div>

Oh, I should acknowledge that **ultrafilter** originally came up with the base 36 idea above, and I forgot about it while writing my post.

Anyway, it just occurred to me that you don’t even need to use a checksumming routine. An even cruder way to distinguish client A from client B would be:  
Six chars + AA = client A  
Six chars + AB = client B  
And so on. You can thus have up to 676 (26[sup]2[/sup]) unique identifiers.

---

<div class="post-metadata">

**Author:** ![Etherman](https://avatars.discourse-cdn.com/v4/letter/e/958977/32.png) [@Etherman](https://boards.straightdope.com/u/Etherman)\
**Post date:** [April 3, 2004, 8:51am UTC](https://boards.straightdope.com/t/mathematical-conundrum/237628/15 "2004-04-03T08:51:56Z")

</div>

> [@Earthling](#):
>
> If you need to distinguish between two clients who may give you duplicate 9-digit numbers, a crude way to accomplish this would be to convert them to a higher number base (so fewer characters result), apply a different checksum algorithm to each client’s data, and place that result in the 8th slot.

Or convert them all to base 36 as per **ultrafilter** , and attach a 3 digit base36 prefix(or postfix) that maps to a company code. Simply pull off the attached 3 chars, convert to base ten, and there you have it.

---

<div class="post-metadata">

**Author:** ![Etherman](https://avatars.discourse-cdn.com/v4/letter/e/958977/32.png) [@Etherman](https://boards.straightdope.com/u/Etherman)\
**Post date:** [April 3, 2004, 8:53am UTC](https://boards.straightdope.com/t/mathematical-conundrum/237628/16 "2004-04-03T08:53:56Z")

</div>

Damn slow preview. Well at least I know it’s a valid idea.

And yeah, 2 char code, not 3 as I said.
