Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 16th, 2012  |  syntax: None  |  size: 0.42 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public int converterParaDecimal (String b) {
  2.                 StringBuffer a = new StringBuffer(b);
  3.                 a.reverse(); //invertendo a String do binário e calculando o valor decimal
  4.                 int k = 0;
  5.                 for (int i = 0; i<a.length(); i++) {
  6.                         if (a.charAt(i) == '1') {
  7.                                 k = (int) (k + Math.pow(2, i)); //procura onde tem 1 e soma as potencias de 2, onde o expoente dessas potencias é a posição do '1' na String.
  8.                         }
  9.                 }
  10.                 return k;
  11.         }