CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2008
    Location
    India
    Posts
    780

    Please chk this ther is some error?

    Hi all,

    i create this code for some conversion but i dont know waht is the mistake is present dere,
    loop is not return,

    please chk this code.

    Code:
    //here i pass
    
    Value=72;
    digits=7;
    
    CString PDU::Bin(int Value, short digits)
    {
    	CString Bin;
    	//#define def_Bin
    //#ifdef def_Bin
    
    	CString result; 
    	short exponent=0;
         // this is faster than creating the string by appending chars
         result = CString("0"[0],32);
    	 do{
    		 if (Value & Power2(exponent)) {
                 // we found a bit that is set, clear it
                 result.Mid(32 - exponent-1, 1);
                 Value = Value ^ Power2(exponent);
    		 }
             exponent += 1;
    	 } while (Value);
    	 if (digits < 0) {
             // trim non significant digits, if digits was omitted or negative
             Bin = result.Mid(33 - exponent-1);
    	 } else {
             // else trim to the requested number of digits
             Bin = result.Right(digits);
    	 }
    
    //#endif // def_Bin
    
    	return Bin;
    
    }
    int PDU::Power2(int exponent)
    {
    	int Power2=0;
    
    //#define def_Power2
    //#ifdef def_Power2
    
    	 static int res[31+1];
         int I;
    
         // rule out errors
         if( exponent < 0 || exponent > 31) /*throw(5);*/
    		 //AfxMessageBox("Invalid");
    
         // initialize the array at the first call
    	 if(res[0] = 0) {
             res[0] = 1;
    		 for(I = 1;I<30;I++) {
                 res[I] = res[I - 1] * 2;
    		 }
             // this is a special case
             res[31] = 0x80000000;
    	 }
    
         // return the result
         Power2 = res[exponent];
    
    //#endif // def_Power2
    
    
    	return Power2;
    
    }
    please help me.
    IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Please chk this ther is some error?

    i create this code for some conversion but i dont know waht is the mistake is present dere,
    Would be nice if you told us what the problem is.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Please chk this ther is some error?

    It would also be nice if you:
    1. removed all unnecessary comments (like commented out lines of code)
    2. explained what CString("0"[0],32) means
    Victor Nijegorodov

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Please chk this ther is some error?

    Code:
    result = CString("0"[0],32);
    Is the same as...
    Code:
    CString ('0', 32);
    Code:
    Value = Value ^ Power2(exponent);
    ^ is a bitwise XOR, not 'power'.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Please chk this ther is some error?

    Quote Originally Posted by Skizmo View Post
    Code:
    result = CString("0"[0],32);
    Is the same as...
    Code:
    CString ('0', 32);
    Wow! Thank you, Skizmo!
    I usually prefer very simple and short expression, so this one:
    Code:
    CString ('0', 32);
    I like better!
    Victor Nijegorodov

  6. #6
    Join Date
    Jun 2005
    Posts
    315

    Re: Please chk this ther is some error?

    loop is not return,
    Does this mean that the do..while loop never exits?

    Using the debugger and stepping through the code (in the do..while loop) should point you to the problem fairly quickly.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Please chk this ther is some error?

    I'd also recommend to not use CString in this algorithm at all (note, that it won't work in the UNICODE build, and you could have some problems because of multiple NULL-characters in it). Use CByteArray instead!
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured