Dec2Bin

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

  • [b][red]This message was edited by stober at 2004-1-26 11:3:45[/red][/b][hr]
    [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
    :



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion