ok, this works fine for number greater than 25, but not for numbers 25 and below. for numbers less than 26 it gets a bunch of weird characters in it.
[code]char * Dec2Bin(int Dec){
char *bin = new char[];
int pos = 0;
while(Dec > 0){
if(Dec % 2 == 0){
bin[pos] = '0';
}else{
bin[pos] = '1';
}
Dec = Dec/2;
pos++;
}
return strrev(bin);
}[/code]
anyone know how to fix this?
Compiler: MS VC++ 6 Enterprise
Platform: MS-DOS
OS: Win ME
Comments
[blue]Works for me after correcting [red]RED[/red] below. I tried values 15, 20 and 80, then compared the results produced by Windows Calculator program.[/blue]
: ok, this works fine for number greater than 25, but not for numbers 25 and below. for numbers less than 26 it gets a bunch of weird characters in it.
:
: [code]char * Dec2Bin(int Dec){
: char *bin = new char[];[red]<<<allocates 0 bytes![/red]
: int pos = 0;
: while(Dec > 0){
: if(Dec % 2 == 0){
: bin[pos] = '0';
: }else{
: bin[pos] = '1';
: }
: Dec = Dec/2;
: pos++;
: }
[red]bin[pos] = 0;[/red]
: return strrev(bin);
: }[/code]
:
: anyone know how to fix this?
:
: Compiler: MS VC++ 6 Enterprise
: Platform: MS-DOS
: OS: Win ME
: