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.
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
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).
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?
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.
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.
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.
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?
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:
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,
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, sample 2). It appears that base 36 needs only 6 spaces max. to convert a 9-digit decimal number. This 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.
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.
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.