Those are two different goals: 1) serialize a general-case string; which can be done using
System.Text.Encoding
, and the result depends on the encoding used; if the encoding represents one of Unicode UTFs, all results will be different but equivalent, and the failure to use UTF can lead to data loss (think '????' characters); 2) serialization of a number represented by a string.
In the second case, it has nothing to do with
string
. You should work with data, not with the strings representing data. The strings can be used only for presentation of data in UI, human-readable files and the like.
Anyway, you can parse a string to any numeric type using their
Parse
or
TryParse
methods. For example:
http://msdn.microsoft.com/en-us/library/system.uint32.parse.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.uint32.tryparse.aspx[
^].
(Please see other numeric types you might need.)
And then, you can serialize numeric value to array of bytes — and back. Please see the class
System.BitConverter
:
http://msdn.microsoft.com/en-us/library/system.bitconverter.aspx[
^].
—SA