7 Replies - 351 Views - Last Post: 01 February 2013 - 06:23 AM Rate Topic: -----

#1 illuss  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 63
  • Joined: 14-April 11

Wrong Hex Value

Posted 31 January 2013 - 07:15 PM

Hello,
I have a string of binary number and I want to convert it into decimal and then output to a file in hexadecimal format as follow:
int main(){
str = "1001110001111111";

oFilePtr = fopen(outFile,"w");
test = bin2dec(str);
printf("%s\n",str);
fprintf(oFilePtr,"0x%0.4X\n",test);  //I was told to use this format for output to file, it gives me a warning btw
return 0;
}

int bin2dec(char* binStr) {
  int decimalval = 0;
  int stringpos;

  for (stringpos=strlen(binStr)-1; stringpos>=0; stringpos--) {
    decimalval = decimalval<<1;    
    if (binStr[stringpos]=='1') decimalval += 1;
  }
  return decimalval;
}


The output from the printf statement is trivial, but somehow the output to the file is 0xFE39. It's obviously not correct...
I'm thinking there is something not right about the int return type, but even it's the error i don't really know how to fix this. I'm fairly new to C language. Would someone please help?

This post has been edited by illuss: 31 January 2013 - 07:16 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Wrong Hex Value

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,166
  • Joined: 05-May 12

Re: Wrong Hex Value

Posted 31 January 2013 - 07:51 PM

It's not an issue of C language or return types, but rather your algorithm.

If you look at your binary number: "1001110001111111"

And then reverse the bits: 1111111000111001

And then group by 4 bits: 1111 1110 0011 1001

And then covert to hex: F E 3 9

And it looks like the number you got. :)

So what does it tell you about your algorithm?
Was This Post Helpful? 1
  • +
  • -

#3 illuss  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 63
  • Joined: 14-April 11

Re: Wrong Hex Value

Posted 31 January 2013 - 08:02 PM

oh my...

so now that you helped me uncover that error, I changed the conditions of the for loop to:
 for (stringpos= 0, stringpos < strlen(binStr)-1; stringpos++) {
    decimalval = decimalval<<1;    
    if (binStr[stringpos]=='1') decimalval += 1;
  }


the output in the file is still incorrect...it gives 0x4E3F

This post has been edited by illuss: 31 January 2013 - 08:03 PM

Was This Post Helpful? 0
  • +
  • -

#4 illuss  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 63
  • Joined: 14-April 11

Re: Wrong Hex Value

Posted 31 January 2013 - 08:13 PM

oh wait, that for loop conditions wasn't what causes the problem was it??
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,166
  • Joined: 05-May 12

Re: Wrong Hex Value

Posted 31 January 2013 - 08:40 PM

If you look at the hex value: 4E3F

In bits: 0100 1110 0011 1111

And the number you were trying to convert was: 1001 1100 0111 1111

What does tell you about your loop condition.

Or to make things clearer:

   4   E   3   F  - your results in hex  
0100111000111111  - your results in binary
 1001110001111111 - your input in binary



I highly recommend either learning how to use debugger to step through your code, or learning how to simulate your code using pen and paper.

This post has been edited by Skydiver: 31 January 2013 - 08:41 PM

Was This Post Helpful? 0
  • +
  • -

#6 illuss  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 63
  • Joined: 14-April 11

Re: Wrong Hex Value

Posted 31 January 2013 - 10:03 PM

I'm really stuck..I've tried to change it around, use pow() function..it didn't really work out so i changed it back to what i have above, and now it outputs a different value from 0x4E3F
it outputs 0xFFFF :(
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,166
  • Joined: 05-May 12

Re: Wrong Hex Value

Posted 01 February 2013 - 05:47 AM

Okay, let me change tacks here. Why do you have strlen(binStr) - 1 as your loop condition?

Are you just randomly throwing code on the screen to see what works or is there an explicit reason for it?
Was This Post Helpful? 0
  • +
  • -

#8 illuss  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 63
  • Joined: 14-April 11

Re: Wrong Hex Value

Posted 01 February 2013 - 06:23 AM

I just woke up from a not very good sleep, but I literally dreamt of the solution lol
that -1 shouldn't be there...it works now, thank you Skydiver!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1