Ascii输入到字符串 [英] Ascii input to string

查看:120
本文介绍了Ascii输入到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始学习ASCII以及它在C#开发中扮演的角色,我想知道如何将ASCII字符转换为字符串?



例如,如果我有(7210110810811132119111114108100)打印出Hello World,我该如何将这些数字转换为可读字符串给用户?



我想到的是以下内容(但显然不起作用):



我有什么试过:



私人无效转换()

{

string string1 =7210110810811132119111114108100 ;

string string2 = Encoding.ASCII.GetString(string1);



}

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);

}

推荐答案

嗯。

该字符串

Um.
That string
string string1 = "7210110810811132119111114108100";

不是ASCII。

ASCII是一个字符集,与UNICODe的方式大致相同,但要小得多。每个字符都有一个值:H是十进制72,或48十六进制,或者(异常这些天)110八进制。您的字符串可能包含一些转换为ASCII值的数字,但它可以读取为ascii字符串,因为数字不以任何方式分隔。好吧我们可以假设它的十进制,因为我们知道第一个字符是'H'并且它以72开头,但是所有1,10和101都是值ASCII字符。分别是SOH,LF和'e'。

可以转换这个字符串,但实际上你需要回到源代码并获取8位字节的值或两个十六进制数字你甚至尝试之前的价值。

如果你有,你可以把它们读作一个字节数组,然后把它转换成可打印的Unicode字符串很简单:

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:

byte[] bytes = ...
string s = System.Text.Encoding.ASCII.GetString(bytes);


Quote:

例如,如果我有(7210110810811132119111114108100)打印出Hello World,

For instance if I have (7210110810811132119111114108100) which would print out Hello World,

你看起来很困惑。暂时忘记这一点并改为正确学习C#。问题(不存在)将自行解决。

You look pretty confused. Forget this for now and learn properly C# instead. the problem (which don't exist) will be resolved by itself.


您的字符串7210110810811132119111114108100是通过将格式为的字符值附加为小数而不带前导零来构造的。因此单个字符可以用1到3位数字表示。不可能将其反转为字符序列(字符串),因为算法无法确定1到3位宽字符值之间的边界。



如果你有 0 72101108108111 0 32119111114108100而将algrithm转换回单词可能在伪现象中看起来像这样-code:

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


这篇关于Ascii输入到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆