将char转换为字符串 [英] Convert char to string

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

问题描述

using .net managed code
char a=''a'';
String^ b=Convert::toString(a);



在上面的代码之后,b等于97.
如何将char转换为非十进制值的字符串?

我想要b ="a".

我发现wchar_t类型数据可以毫无问题地转换为它.



After above code, b is equal to 97.
How to I convert a char to a string that is not a decimal value?

I want b="a".

I found that a wchar_t type data can convert to it without any problem.

wchar_t a=''a'';
String^ b=Convert::toString(a);





Must I use wchar_t type to solve my problem?

推荐答案

是的,您应该使用宽字符(因为.NET框架使用它们).
至于您的示例,您也可以这样写:
Yes, you should use wide characters (because the .NET framework uses them).
As about your example, you may also write:
char a='a';
String^ b=Convert::toString((wchar_t)a)


:)


这些天我不知道他们在学校教什么,但我对那些似乎对信息方式知之甚少的人问的数量感到惊讶不已存储在计算机的内存中以及如何显示.

值97只是内存中包含字符"a"的字节的十进制表示.以十六进制显示为x61.字符串的第一个字符为字符"a"(97或x61),下一个字符为空字节(0或x0),表示字符串的结尾.
I don''t know what they teach in school these days but I never cease to be amazed at the number of questions from people who appear to have very little understanding of how information is stored in a computer''s memory and how it is displayed.

The value 97 is merely the decimal representation of a byte in memory containing the character ''a''. In hex it will appear as x61. The first character of your string will be the character ''a'' (97 or x61) and the next character will be a null byte (0 or x0) indicating the end of the string.


在上面Signor Pallini的评论之后,我再次审视了这个问题,并为我的(某些)咆哮道歉,因为它本应该针对Microsoft.

C#(我假设使用VB.NET)将char用作System::Char的别名. C ++/CLI使用char作为旧的C型字符,并使用wchar_tWCHAR作为System::Char的别名.和作为Sgr. P.请注意,Convert.ToString(char)char值用作整数并将其转换为97.

C ++/CLI并不是一个好主意的另一个原因.
Following Signor Pallini''s comments above I have taken another look at the issue and apologise for (some of) my rant, as it should have been directed at Microsoft.

C# (and VB.NET I assume) uses char as an alias for System::Char. C++/CLI uses char as the old C-type character, and uses wchar_t or WCHAR as aliases for System::Char. And as Sgr. P. notes, Convert.ToString(char) takes the char value to be an integer and thus converts it to 97.

Yet another reason why C++/CLI is not a good idea.


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

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