Note that there are some explanatory texts on larger screens.

plurals
  1. POchar to system.string in windowsforms
    text
    copied!<p>I wrote a program to write numbers in different bases (base 10, binary, base 53, whatever...)</p> <p>I inicially wrote it as a win32 console application, in visual c++ 2010, and then converted it to a Windows Form Application (I know, I know...)</p> <p>In the original form, it worked perfectly, but after the conversion, it stopped working. I narrowed down the problem to this:</p> <p>The program uses a function that receives a digit and returns a char:</p> <pre><code>char corresponding_digit(int digit) { char corr_digit[62] = {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P' ,'Q','R','S' ,'T','U','V','W','X','Y','Z', 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p' , 'q','r','s' ,'t','u','v','w','x','y','z'}; return corr_digit[digit]; } </code></pre> <p>This function takes numbers from 1 to 61, and return the corresponding character: 0-9 -> 0-9; 10-35 -> A-Z; 36-61 a->z.</p> <p>The program uses the function like this:</p> <pre><code>//on a button click //base is an integer got from the user String^ number_base_n = ""; if(base == 10) number_base_n = value.ToString(); else if(base==0) number_base_n = "0"; else { int digit, power, copy_value; bool number_started = false; copy_value = value; if(copy_value &gt; (int)pow(float(base), MAX_DIGITS)) //cmath was included number_base_n = "Number too big"; else { for(int i = MAX_DIGITS; i &gt;= 0; i--) { power = (int)pow(float(base), i); if(copy_value &gt;= power) { digit = copy_value/power; copy_value -= digit*power; number_started = true; number_base_n += corresponding_digit(digit); } else if (number_started || i==0) { number_base_n += "0"; } } } } textBox6-&gt;Text = number_base_n; </code></pre> <p>After debugging a bit, I realized the problem happens when function corresponding_digit is called with digit value "1", which should return '1', in the expression</p> <pre><code>//number base_n equals "" number_base_n += String(corresponding_digit(digit)); //number_base_n equals "49" </code></pre> <p>number_base_n, starting at "", ends with "49", which is actually the ASCII value of 1. I looked online, and all I got was converting the result, with String(value) or value.ToString(), but apparently I can't do</p> <pre><code>number_base_n += corresponding_digit(digit).ToString(); </code></pre> <p>I tried using an auxiliar variable: </p> <pre><code>aux = corresponding_digit(digit); number_base_n += aux.ToString(); </code></pre> <p>but I got the exact same (wrong) result... (Same thing with String(value) )</p> <p>I fumbled around a bit more, but not anything worth mentioning, I believe. So... any help?</p> <p><em>Also: base 10 and base 0 are working perfectly</em></p> <p>Edit: If the downvoter would care to comment and explain why he downvoted... Constructive criticism, I believe is the term.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload