Click here to Skip to main content
15,888,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I recently started learning about ASCII and what role it plays in C# development and I was wondering how can I convert ASCII characters into a string?

For instance if I have (7210110810811132119111114108100) which would print out Hello World, how would I convert these numbers into readable character string to the user?

What I had in mind is the following (but it obviously does not work):

What I have tried:

private void conversion()
{
string string1 = "7210110810811132119111114108100";
string string2 = Encoding.ASCII.GetString(string1);

}
Posted
Updated 10-Mar-16 8:47am
Comments
Sergey Alexandrovich Kryukov 10-Mar-16 14:56pm    
Too late. C# is based only on Unicode, even for the characters which fall into the ASCII range. ASCII is only supported as a special case, in streams, etc.

Now, you are mixing up different things. ""7210110810811132119111114108100" is not ASCII, this is a string (Unicode string), which may or may not represent numbers ("72" is not a number, 72 is), which may or may not represent ASCII code points. You are doing wrong thing. You have to break the string into sub-strings like "72", "10"..., convert each to integer number (or the type byte) using byte.Parse(string), and then simply cast each number to char.

—SA

Um.
That string
C#
string string1 = "7210110810811132119111114108100";
Isn't ASCII.
ASCII is a character set, in much the same way as UNICODe is, but a lot smaller. Each character has a value: H would be 72 decimal, or 48 Hex, or (unusually these days) 110 in Octal. Your string may contain some numbers which translate to ASCII values, but it can;t be read as a string of ascii characters because the numbers aren't delimited in any way. OK we can assume its Decimal, becuase we "know" that teh first character is 'H' and it starts with "72", but all of "1", "10", and "101" are value ASCII characters. SOH, LF, and 'e' respectively.
It's possible to convert this string, but really you need to go back to the source and either get the values as 8 bit bytes, or as two hex digit values before you even try.
If you have either, you can read them as an array of bytes, and then its trivial to convert that to a printable Unicode string:
C#
byte[] bytes = ...
string s = System.Text.Encoding.ASCII.GetString(bytes);
 
Share this answer
 
Quote:
For instance if I have (7210110810811132119111114108100) which would print out Hello World,
You look pretty confused. Forget this for now and learn properly C# instead. the problem (which don't exist) will be resolved by itself.
 
Share this answer
 
Your string "7210110810811132119111114108100" is constructed by appending the character values formatted as decimals without leading zeroes. So a single character could be represented by 1 to 3 digits. It's impossible to reverse this to a character sequence (string) because an algorithm couldn't determine the boundaries between the 1 to 3 digits wide character-values.

If you had "072101108108111032119111114108100" instead an algrithm to convert this back into "words" could look like this in pseudo-code:
outputstring = empty string
groups = split inputstring into groups of 3 chars
foreach group in groups
   int value = parse group into an integer
   char character = cast value to character
   append character to outputstring
end foreach
return outputstring
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900