简体   繁体   English

C#使用强制转换将Int64转换为String

[英]C# Convert Int64 to String using a cast

How do you convert an integer to a string? 如何将整数转换为字符串? It works the other way around but not this way. 它以相反的方式起作用,但不是这样。

string message
Int64 message2;
message2 = (Int64)message[0];

If the message is "hello", the output is 104 as a number; 如果消息为“ hello”,则输出为104;

If I do 如果我做

string message3 = (string)message2;

I get an error saying that you cant convert a long to a string. 我收到一条错误消息,说您不能将long转换为字符串。 Why is this. 为什么是这样。 The method .ToString() does not work because it converts just the number to a string so it will still show as "104". .ToString()方法不起作用,因为它仅将数字转换为字符串,因此仍显示为“ 104”。 Same with Convert.ToString(). 与Convert.ToString()相同。 How do I make it say "hello" again from 104? 如何使104再次说“你好”? In C++ it allows you to cast such methods but not in C# 在C ++中,它允许您强制转换此类方法,但在C#中则不允许

message[0] gives first letter from string as char , so you're casting char to long , not string to long . message[0]给出string中的第一个字母为char ,因此您将charlong ,而不是stringlong

Try casting it back to char again and then concatenate all chars to get entire string. 尝试再次将其强制转换回char ,然后将所有char连接起来以获取整个字符串。

ToString() is working exactly correctly. ToString()完全正确地工作。 Your error is in the conversion to integer. 您的错误在于转换为整数。

How exactly do you expect to store a string composed of non-numeric digits in a long? 您期望如何精确地将由非数字组成的字符串长时间存储? You might be interested in BitConverter , if you want to treat numbers as byte arrays. 如果您想将数字视为字节数组,则可能对BitConverter感兴趣。

If you want to convert a numeric ASCII code to a string, try 如果要将数字ASCII码转换为字符串,请尝试

((char)value).ToString()

Another alternative approach is using ASCII.GetBytes method as below 另一种替代方法是使用ASCII.GetBytes方法,如下所示

string msg1 ="hello";
byte[] ba = System.Text.Encoding.ASCII.GetBytes(msg1);
//ba[0] = 104 
//ba[1] = 101 
//ba[2] = 108 
//ba[3] = 108 
//ba[4] = 111 

string msg2 =System.Text.Encoding.ASCII.GetString(ba);
//msg2 = "hello"

Try this method: 试试这个方法:

string message3 = char.ConvertFromUtf32(message2);
  • 104 is the value of "h" not "hello". 104是“ h”而不是“ hello”的值。

There is no integer representation of a string, only of a char. 没有字符串的整数表示,只有一个char。 Therefore, as stated by others, 104 is not the value of "hello" (a string) but of 'h' (a char) (see the ASCII chart here ). 因此,正如其他人所述,104不是“ hello”(字符串)的值,而是“ h”(字符)的值(请参见此处ASCII图表 )。

I can't entirely think of why you'd want to convert a string to an int-array and then back into a string, but the way to do it would be to run through the string and get the int-value of each character and then reconvert the int-values into char-values and concatenate each of them. 我无法完全想到为什么要将字符串转换为int数组,然后再转换回字符串,但是要做到这一点的方法是遍历字符串并获取每个字符的int值然后将int值重新转换为char值并将它们连接起来。 So something like 所以像

string str = "hello"
List<int> N = new List<int>();
//this creates the list of int-values
for(int i=0;i<str.Count;i++)
  N.Add((int)str[i]);
//and this joins it all back into a string
string newString = "";
for(int i=0;i<str.Count;i++)
  newString += (char)N[i];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM