C++ strange output converting string to int -


I am writing a program that converts binary string to decimal. Before I actually started this method, I wanted to validate my output. I have the following code:

  int get_val () {int sum = 0; (Int new_bits = size; num_bits> 0; num_bits--) {printf ("string sub ii is% i \ n", num_bits, int (bin [num_bits])); }}  

When I input a string of 16 zeros, I get the following output:

  string sub 16 24 string sub 15 string sub Is 14 is 0 string is sub 13 is string sub 12 23 string sub 11 is 0 string is sub 10 is 0 string sub 9 is 0 string sub 8 22 string sub 7 is 0 string sub 6 string sub 5 is 0 string sub 4 21 string sub 3 is 0 string is 2 is 0 string is sub 1 is 0  

If I input all the hards So why would I get different prices?

Edit: bin "0000000000000000"

until the question is updated So maybe this example code helps. It changes a binary string in an integer. I tried to keep my code and variable name as much as possible.

  #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; #include & lt; String & gt; using namespace std; Int main () {string bin = "000111010"; Int size = bin.length (); Int sum = 0; {Sum & lt; for num (num_bits = 1; num_bits & lt; = size; num_bits ++) & Lt; = 1; Sum + = bin [num_bits - 1] - '0'; } Printf ("The converted binary string is% s:% I \ n", bin.c_str (), yoga); } As already mentioned in the comment, here the main challan is to convert the ASCII character '0' and '1' to integers 0 and 1, which by decreasing the value Is done '0'. In addition, I have changed the string order of the string because in this way, you can change the integer after each bit and always set the value of the current minimum bit. 


Comments