|
Thanks
|
|
|
|
|
Anybody have a better date time parse algorithm? I've tried DateTime.TryParse, but it doesn't pick up a lot of conditions. For example 1:05P != 1:05PM because it doesn't know what P is.
|
|
|
|
|
I know it's not C#, but take a look at date.js[^]
It's open source, so if you like what you can do in the sandbox on that page, maybe you could hack it to c#. Or join the project.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Define 'better'. Date parsing in particular suffers from ambiguity if you try to be too vague (i.e. what date is 6-3? what about 6-3-12?). It's generally better to mandate a format and make users enter in that format.
|
|
|
|
|
Values can have up to 1000.
How to represent numbers up to 1000 within the 2 alphanumeric placeholder?
Eg., 100, 976, 1000 should all be converted to 2 alphanumeric placeholders.
There will be a C# class that converts the values back and forth.
Has anyone had any need to do this in the past?
You can never try. You either do it or you don't.
|
|
|
|
|
No - but it isn't at all difficult.
If you allow A-Z and 0-9 only, you can fit 36 x 36 items in two characters - 1296 different values.
So divide your number into two parts : integer divide by 36, and modulo 36 and use them as an offset into a character wide translation table.
Reassembly is the reversal of disassembly.
[edit]Phrasing change - the original sounded like the table should be two character wide - OriginalGriff[/edit]
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Thank you...
I just came across this. Works like magic!
public String ConvertToBase(int num, int nbase)
{
String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (nbase < 2 || nbase > chars.Length)
return "";
int r;
String newNumber = "";
while (num >= nbase)
{
r = num % nbase;
newNumber = chars[r] + newNumber;
num = num / nbase;
}
newNumber = chars[num] + newNumber;
return newNumber;
}
You can never try. You either do it or you don't.
|
|
|
|
|
A StringBuilder might be a better overall technique if you need more digits in the future.
|
|
|
|
|
You could simplify that quite a bit, assuming you don't plan on increasing the field size later...
const string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static readonly int num_chars = chars.Length;
static string NumberToCode(int number)
{
if (number < 0 || number > num_chars * num_chars)
throw new ArgumentOutOfRangeException();
return chars[number / num_chars].ToString() + chars[number % num_chars];
}
|
|
|
|
|
You only need base 32, so you can throw some letters out. In case there are some letters that you just don't like or something like that.
As a bonus, the math is easier for 32 (not that it matters in this case..)
|
|
|
|
|
In practice, I would probably throw out "O" and "I", possibly "Z" as well just to make it clearer to people reading it.
But then, I try to avoid this kind of thing anyway!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
If you convert your numeric values to base 36 you can display the values using two characters.
BDF
I often make very large prints from unexposed film, and every one of them turns out to be a picture of myself as I once dreamed I would be.
-- BillWoodruff
|
|
|
|
|
|
I'd use Base-64, but that's just me.
|
|
|
|
|
Good point. I would do the same as well.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
static public string EncodeTo64(string toEncode)
{
byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
static public string DecodeFrom64(string encodedData)
{
byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
return returnValue;
}
Base 64 seems to be represented in 4 position format as shown below. What am I missing?
BASE 64:
From Number 0 to Base 64:MA== ~ From Base 64 MA== to Number: 0
From Number 1 to Base 64:MQ== ~ From Base 64 MQ== to Number:1
From Number 2 to Base 64:MG== ~ From Base 64 MG== to Number: 0
From Number 3 to Base 64:MW== ~ From Base 64 MW== to Number:1
From Number 4 to Base 64:NA== ~ From Base 64 NA== to Number:4
From Number 5 to Base 64:NQ== ~ From Base 64 NQ== to Number:5
From Number 6 to Base 64:NG== ~ From Base 64 NG== to Number:4
From Number 7 to Base 64:NW== ~ From Base 64 NW== to Number:5
From Number 8 to Base 64:OA== ~ From Base 64 OA== to Number:8
From Number 9 to Base 64:OQ== ~ From Base 64 OQ== to Number:9
From Number 10 to Base 64:MTA= ~ From Base 64 MTA= to Number:10
From Number 11 to Base 64:MTE= ~ From Base 64 MTE= to Number:11
From Number 12 to Base 64:MTI= ~ From Base 64 MTI= to Number:12
From Number 13 to Base 64:MTM= ~ From Base 64 MTM= to Number:13
Base 36
From Number 71 to Base36:1Z ~ From Base36 1Z to Number:71
From Number 72 to Base36:20 ~ From Base36 20 to Number:72
From Number 73 to Base36:21 ~ From Base36 21 to Number:73
From Number 74 to Base36:22 ~ From Base36 22 to Number:74
From Number 75 to Base36:23 ~ From Base36 23 to Number:75
From Number 76 to Base36:24 ~ From Base36 24 to Number:76
From Number 77 to Base36:25 ~ From Base36 25 to Number:77
From Number 78 to Base36:26 ~ From Base36 26 to Number:78
From Number 79 to Base36:27 ~ From Base36 27 to Number:79
From Number 80 to Base36:28 ~ From Base36 28 to Number:80
From Number 81 to Base36:29 ~ From Base36 29 to Number:81
From Number 82 to Base36:2A ~ From Base36 2A to Number:82
From Number 83 to Base36:2B ~ From Base36 2B to Number:83
From Number 84 to Base36:2C ~ From Base36 2C to Number:84
From Number 85 to Base36:2D ~ From Base36 2D to Number:85
From Number 86 to Base36:2E ~ From Base36 2E to Number:86
From Number 87 to Base36:2F ~ From Base36 2F to Number:87
From Number 88 to Base36:2G ~ From Base36 2G to Number:88
From Number 89 to Base36:2H ~ From Base36 2H to Number:89
From Number 90 to Base36:2I ~ From Base36 2I to Number:90
You can never try. You either do it or you don't.
|
|
|
|
|
That type of Base-64 is for encoding text, not numbers. I meant something like what was posted but with sixty-four digits. Rather than arguing about whether to use Base-32 or Base-36, I'd just use Base-64.
|
|
|
|
|
In my world 4-digit numbers often go all the way to 9999, and then most answers aren't solving the problem.
|
|
|
|
|
I guess his world is a lot smaller.
BDF
I often make very large prints from unexposed film, and every one of them turns out to be a picture of myself as I once dreamed I would be.
-- BillWoodruff
|
|
|
|
|
Yeah, something tells me it's the size of a class room. If that's true, I hope his prof finds out the code he turned in is not his own work.
|
|
|
|
|
Hehe.
He did specify up to 1000 though.
You'd need 100 unique characters to get 10,000 unique values into 2 characters. That's easily possible within the 224 available ANSI one-byte codepoints, though sadly not quite within the 96 7-bit ones.
|
|
|
|
|
sfdsfs
modified 9-Oct-12 11:01am.
|
|
|
|
|
Nice code dump
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
no formatting, no question; what is this?
|
|
|
|
|