|
-
March 15th, 2010, 12:50 AM
#1
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
-
March 15th, 2010, 03:02 AM
#2
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.
-
March 15th, 2010, 06:30 AM
#3
Re: Please chk this ther is some error?
It would also be nice if you:
- removed all unnecessary comments (like commented out lines of code)
- explained what CString("0"[0],32) means
Victor Nijegorodov
-
March 15th, 2010, 07:04 AM
#4
Re: Please chk this ther is some error?
Code:
result = CString("0"[0],32);
Is the same as...
Code:
Value = Value ^ Power2(exponent);
^ is a bitwise XOR, not 'power'.
-
March 15th, 2010, 07:28 AM
#5
Re: Please chk this ther is some error?
 Originally Posted by Skizmo
Code:
result = CString("0"[0],32);
Is the same as...
Wow! Thank you, Skizmo!
I usually prefer very simple and short expression, so this one:I like better!
Victor Nijegorodov
-
March 15th, 2010, 12:00 PM
#6
Re: Please chk this ther is some error?
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.
-
March 15th, 2010, 12:09 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|