In my case I needed to add a new value to the registry. The data is different for each host and the input file lists the data values as a hexadecimal string. The type accelerators for numeric conversion don't help in this case
How to crack that nut? When in doubt, check if .net has any tools for us to use. In this case there is a quite powerful system class named, appropriately enough, convert. This is the swiss army knife of data conversion letting us convert just about any concievable combination of data classes.
In my case I needed to use one of the ToInt methods. Registry values will be either 32 or 64 bit (dword or qword) but 64 bit will only work with newer versions of Windows.
Notice how many different possible inputs I have for ToInt32. The one I need is the one that accepts a string and specifies the base of the string. There are versions of this for 16 and 64 bit integers as well. This example shows the string '1010' converted from the available bases, binary, octal, decimal, and hexadecimal.
Most of the conversion methods are straightforward and self explanitory. The ToByte method could have worked if the hex values were small enough to fit into a single byte.
ToBoolean converts any number isn't a zero to true. And it can interpret any string that evaluates to 'true' or 'false'. This would be handier if it could automatically convert numeric strings to numbers or evaluate logical equivilants like 'yes' or 'no' to 'true' or 'false'.
The ToBase64 and FromBase64 methods allow some quick encoding and decoding of strings and character arrays. Depending on the input you may have to do some massaging of the data because the ToBase64 method expects an array of bytes as input.
The GetTypeCode returns a value describing the data type of the input object.
This information comes in handy if you want to use the ChangeType method.
But that's a long way to go because we have the shortcut of using the type accelerators. You will almost always use the built-in type accelerators but, as in dating, its nice to know you have other options.
No comments:
Post a Comment