Mark said:
Thanks. I have re-coded as below and mTest() works -- it returns the
current date/time. I have a few questions:
1) It always seems to put two equal signs at the end of the string no
matter what: "BJ+UEg==". Should I truncate them from the URL since they
seem to be superfluous and then add them back later when perform
mConvertBase64StringToUTCDateTime()?
Yes. For details, see
http://tools.ietf.org/html/rfc3548
Basically, the '=' character is used to make sure the data is a multiple
of 24 bits (LCM of 6 and 8). Since you know the length of the data in
advance, it's safe to not store the padding, replacing it before
converting back (actually, it's possible the FromBase64String() method
will fill it in as necessary if you pass it an incomplete string...I
don't remember, but it's worth a try).
2) The PC's that use the code could be anywhere in the world with any
Language setting (e.g Chinese, Arabic, English etc) . I'll assume the
code below would be Region independant?
Yes. While the string returned is Unicode (just like any .NET string),
it will always use the characters from the range 32-127; i.e. valid
ASCII characters.
3) I saw some code comments on the net that said the LittleEndian test
was needed in case the system archicture is different. As above, the
users could be on different PC's around the world. Do I need to
uncomment the code and if so would I then need to put it in
mConvertBase64StringToUTCDateTime() too?
It depends on the context. If this is data that can cross between
architectures, then yes...you should detect and handle endianness
correctly. But, if you're embedding it in a URL, that suggests the same
entity (e.g. web server) that issues the encoded data is going to be the
entity that receives and decodes that data later.
If you do decide you need to handle endianness, keep in mind:
-- Little-endian is more common in the .NET world, so unless you
have specific knowledge to the contrary about the prevalent architecture
your code will run on, you should probably encode the data as
little-endian, swapping bytes only if the architecture is big-endian.
-- Here, you are encoding just a single 32-bit integer, and so
reversing the entire byte array is fine. But obviously if you apply
this technique to longer collections of data, you need to make sure you
are reversing bytes at the individual data element level (e.g. every 4
bytes if it's an array of 32-bit integers, etc.).
Pete