Ver Mensaje Individual
  #2 (permalink)  
Antiguo 30/01/2016, 02:14
Avatar de Profesor_Falken
Profesor_Falken
 
Fecha de Ingreso: agosto-2014
Ubicación: Mountain View
Mensajes: 1.323
Antigüedad: 9 años, 8 meses
Puntos: 182
Respuesta: De ASCII to bytes

Buenas,

Pues no lo entiendo, porque a mi me funciona perfectamente. Este es mi test:

Código Java:
Ver original
  1. public class TestAscii {
  2.     public static void main(String[] args) {
  3.         System.out.println(AsciiToBinary("ª"));
  4.         System.out.println(AsciiToBinary("Ø"));
  5.         System.out.println(AsciiToBinary("("));
  6.     }
  7.    
  8.     public static String AsciiToBinary(String asciiString){  
  9.  
  10.           byte[] bytes = asciiString.getBytes();  
  11.           StringBuilder binary = new StringBuilder();  
  12.           for (byte b : bytes)  
  13.           {  
  14.              int val = b;  
  15.              for (int i = 0; i < 8; i++)  
  16.              {  
  17.                 binary.append((val & 128) == 0 ? 0 : 1);  
  18.                 val <<= 1;  
  19.              }  
  20.           }  
  21.           return binary.toString();  
  22.     }
  23. }

Resultado:
10101010
11011000
00101000

http://www.ascii-code.com/


No estaras enviando al metodo accidentalmente dos caracteres en lugar de uno?


Un saludo
__________________
If to err is human, then programmers are the most human of us