Get the numeric value of all characters in a string

I want to get the decimal value of an ASCII string in C #, for example:

"abc123s"will be: 97 98 99 49 50 51 115 (979899495051115) 'f1c3s1 " would be:102 49 99 51 115 49 or1024999511549`

I tried Convert.ToInt32or Int.Parse, but they do not produce the desired effect.

What method can I use for this?

+4
source share
3 answers

Assuming that you only work with ASCII strings, you can drop every character stringbefore byteto get an ASCII representation. You can return the results back to stringusing ToStringfor the values:

string str = "abc123s";
string outStr = String.Empty;

foreach (char c in str)
    outStr += ((byte) c).ToString();

byte string String.Join:

byte[] asciiVals = System.Text.Encoding.ASCII.GetBytes(str);
outStr = String.Join(String.Empty, asciiVals);
+2

Convert.ToInt16() int Results .

Byte, Char Int16.

0

Try

string all = "";

all = String.Join(String.Empty, "abc123s".Select(c => ((int)c).ToString()).ToArray());
0
source

All Articles