2个字符到1个整数 - 然后返回。 [英] 2 char's to 1 integer - and back.

查看:58
本文介绍了2个字符到1个整数 - 然后返回。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




是否可以将2个字符值转换为整数并且可以反转

呢?


我肯定必须有一些算法来实现这个。


有什么想法吗?

谢谢

Kev

解决方案

Kev,


你的意思是''1''和''0''产生整数10?

如果是这样,只需创建一个新的字符串实例,然后将其传递给Int32类上的静态

Parse方法。 br />

要反转操作,请在int上调用ToString,然后在字符串实例上调用

ToChars来获取字符。

希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" Mantorok" < no ** @non.comwrote in message

news:eu ********** @ newsfeed.th.ifl.net ...





是否可以将2个char值转换为整数并且能够将b $ b转换为b?


我肯定必须有一些算法来实现这个。


任何想法?

谢谢

Kev



对不起,我应该更清楚。


我的意思是如果我有''A''和''B'',试图将它们转换为整数然后

返回。


Kev


Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com写在

消息新闻:u6 ************** @ TK2MSFTNGP05.phx.gbl ...


Kev,


你的意思是''1'和'0'来产生整数10?


如果是这样,只需创建一个新的字符串实例,然后将其传递给Int32类上的静态

Parse方法。


要反转操作,请在int上调用ToString,然后在字符串实例上调用

ToChars来获取字符。


希望这有助于。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Mantorok" < no ** @non.comwrote in message

news:eu ********** @ newsfeed.th.ifl.net ...


>嗨

是否可以将2个char值转换为整数并且能够将其反转?

我肯定必须有一些算法来实现这个。

任何想法?
谢谢
Kev




" Mantorok" < no ** @non.comwrote in message

news:eu ********** @ newsfeed.th.ifl.net ...


是否可以将2个char值转换为整数并且能够将b $ b反转为它?


我''我肯定必须有一些算法来实现这个目标。



让我们说有问题的两个字符是a和Z......


char [] charArray = {'''',''Z''};


int int1 =(int)charArray [0]; // 97

int int2 =(int)charArray [1]; // 90

string string1 = int1.ToString(); //" 97"

string string2 = int2.ToString(); //" 90"


string strFinal = string1 + string2 + string1.Length; //" 97902"

int intFinal = int.Parse(strFinal); // 97902

最后的数字,在这种情况下为2,是校验位。即它告诉你

在哪里拆分字符串...


所以,要将其反转,将其转换为字符串,将其拆分为校验位,然后

转换为整数值回到字符...


Hi

Is it possible to convert 2 char values to an integer and be able to reverse
it?

I''m sure there must be some algorithm to achive this.

Any ideas?
Thanks
Kev

解决方案

Kev,

Do you mean ''1'' and ''0'' to produce the integer 10?

If so, just create a new string instance and then pass it to the static
Parse method on the Int32 class.

To reverse the operation, call ToString on the int, and then call
ToChars on the string instance to get the characters.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mantorok" <no**@none.comwrote in message
news:eu**********@newsfeed.th.ifl.net...

Hi

Is it possible to convert 2 char values to an integer and be able to
reverse it?

I''m sure there must be some algorithm to achive this.

Any ideas?
Thanks
Kev



Sorry, I should''ve been clearer.

I mean if I had ''A'' and ''B'', trying to convert them to integers and then
back.

Kev

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:u6**************@TK2MSFTNGP05.phx.gbl...

Kev,

Do you mean ''1'' and ''0'' to produce the integer 10?

If so, just create a new string instance and then pass it to the static
Parse method on the Int32 class.

To reverse the operation, call ToString on the int, and then call
ToChars on the string instance to get the characters.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mantorok" <no**@none.comwrote in message
news:eu**********@newsfeed.th.ifl.net...

>Hi

Is it possible to convert 2 char values to an integer and be able to
reverse it?

I''m sure there must be some algorithm to achive this.

Any ideas?
Thanks
Kev




"Mantorok" <no**@none.comwrote in message
news:eu**********@newsfeed.th.ifl.net...

Is it possible to convert 2 char values to an integer and be able to
reverse it?

I''m sure there must be some algorithm to achive this.

Let''s say the two chars in question are ''a'' and ''Z''...

char[] charArray = {''a'', ''Z''};

int int1 = (int)charArray[0]; // 97
int int2 = (int)charArray[1]; // 90

string string1 = int1.ToString(); // "97"
string string2 = int2.ToString(); // "90"

string strFinal = string1 + string2 + string1.Length; // "97902"
int intFinal = int.Parse(strFinal); // 97902
The final digit, in this case 2, is the "check-digit" i.e. it tells you
where to split the "string"...

So, to reverse it, convert it to a string, split it at the check digit, then
convert to to integer values back to chars...


这篇关于2个字符到1个整数 - 然后返回。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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