2013-05-20 145 views
0

我正在写一个字符串“123124125”转换为这样的长度为3的整数数组的程序:如何将字符串数字转换为int然后char?

int[0] = 123 
int[1] = 124 
int[2] = 125 

假设密文字符串有123124125.

我用:

int number[100]; 
int length1 = ciphertext-> Length; 
int count = 0; 
int count1 = 0; 
char w[100]; 
while (count1 < length1) 
{ 
    number[count] = (ciphertext[count1] * 100) + (ciphertext[count1 + 1] * 10) + ciphertext[count1 + 2]; 
    count++; 
    count1 = count1 + 3; 
} 

然后我想用这个公式来解密它并直接转换成字符串:

for (int i = 0; i < sizeof(number); i++) 
{ 
    String^demessage += Convert::ToChar(number[i]^(int(key) * 2 + int(key)/int(key)) % 255); 
} 

但它显示了以下结果:

5553195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195195 -................................ .... 1849664615

我哪里错了? 我是否需要先将int转换为char,然后继续执行公式?

谢谢。非常感谢您的帮助。

+0

我认为你得到的数字是你想要的字符的数字表示。我对API不熟悉,但我会做一些非常简单的事情,例如确认添加Convert :: ToChar(65)会产生预期的'a'字符。如果你没有正确使用,你将无法调试其他任何东西。 – cyborg

+0

我可能发现了这个问题,可能是这个公式: number [count] =(ciphertext [count1] * 100)+(ciphertext [count1 + 1] * 10)+ ciphertext [count1 + 2]; count ++; 字符串“123124125”,通过使用上面的公式,它不能得到int [0] = 123,int [1] = 124 int [2] = 125, 和我出来很多方式仍然无法工作,有人可以帮忙吗? –

回答

0

我是一个C#-developer,所以语法可能有点不正确:

试试这个:

String^demessage = ""; 
for (int i = 0; i < sizeof(number); i++) 
{ 
    demessage += number[i].ToString("000"); 
} 

这样,每一个数字转换为字符串表示。因此number[0]的值为123将被转换为字符串"123"并添加到demessage

+0

Hi, number [count] =(ciphertext [count1] * 100)+(ciphertext [count1 + 1] * 10)+ ciphertext [count1 + 2]其实我想从字符串得到“1”,它不会(1 * 100)但它(50 * 100),我想得到100而不是5000,但不能得到它...我怎么能得到它“1”,然后可以与100倍? –

+0

我无法跟随你。请重写你的问题。在你的问题中,请说明两件重要的事情:1)你期待的结果和2)你现在正在得到的(不正确的)结果。比我们可以帮助你更好。 –

+0

是的,我会在另一个线程发布我的新问题...... :) –