二进制加法2值重新$ P $的psented字符串 [英] Binary addition of 2 values represented as strings

查看:137
本文介绍了二进制加法2值重新$ P $的psented字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字符串:

string a = "00001"; /* which is decimal 1 I've converted with next string:
string a = Convert.ToString(2, 2).PadLeft(5, '0'); */
string b = "00010";

我想执行两者之间的二进制加法所以答案是00011(3)。

I want to perform binary addition between the two so the answer will be 00011 ( 3).

推荐答案

System.Convert应该能够做到为你工作。

System.Convert should be able to do the work for you

int number_one = Convert.ToInt32(a, 2);
int number_two = Convert.ToInt32(b, 2);

return Convert.ToString(number_one + number_two, 2);

(您可能需要调整字符串的一个位)

(you may have to tune the strings a bit)

这篇关于二进制加法2值重新$ P $的psented字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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