• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to convert from binary to int?  RSS feed

 
Tris Rabar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ...
Is there a method to convert a binary number to an integer?
Thanks!
 
Greg Brouelette
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you representing a binary? String? char?
Is your question actually this: "How do I convert a string of this format "10010110" into an int?"
If so then simply start at the right most character and add up the powers of 2. Example:
This string
1101
can be represented this way:
1*(2^3) + 1*(2^2) + 0* (2^1) + 1*(2^0)
(see the pattern?)
which is the same as 8 + 4 + 0 + 1 = 13
 
Tris Rabar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Greg..
Actually, this IS what I'm looking for..converting a string of 1's and 0's to an integer. Thanks a lot.
 
Tris Rabar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI...
Sorry to bother you again, but I've tried the following code and it doesn't seem to work. Can you explain why to me please? (I'm sure it's some lil problem)

Thanks...
 
Tris Rabar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again...Sorry, I left out a line in the above code. It should read:

Thanks!!
 
Jeff Smith
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't it just be easier to do something like:

Jeff
 
Tris Rabar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Jeff...thanks for the suggestion.
I ended up using

and it works fine.
Thanks all for your help. If there may be glitches with the above statement, can you please let me know?
Thanks again..
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!