byteArrayToAsciiString gibt mir komische Zeichen ?

Code46

Erfahrenes Mitglied
Hi Leute,

ich habe ein Problem in meinem Program. Undzwar versuche ich einen byteArray in String um zuwandeln und es funktioniert auch. Jedoch bekomme ich sowas hier: -Ò meintext

Output:
Code:
Something received:  -Ò aa968@aaa.de

Dies wird nur im prompt angezeigt, doch ich versuche gleichzeitig auch es im TextArea auszugeben und dies funktioniert auch nicht.

Code:
        try {
			        
			        	byte[] targetConnect = {(byte)0x01,(byte)0x00, (byte) 0x00 };
			        	byte[] reply=transceive(IN_DATA_EXCHANGE, targetConnect);
			        
			        	System.out.println(reply.length + " ");
			        	
			        	if ((byte)reply.length>7){
							System.out.println("Something received:"+Util.byteArrayToAsciiString(reply));
							received = Util.byteArrayToAsciiString(reply);	
                                                        break;
							}	
			        } catch (IsmbSnepException e) {e.printStackTrace();}
				}


Code:
public static String byteArrayToAsciiString(byte[] a) {
            if (a == null)
                    return "[null]";
            if (a.length == 0)
                    return "[empty]";
            String result = "";
            
            for (int i = 0; i < a.length; i++) {            	
                    result = result + (char)(a[i] & 0xff);
            }
            return result;



Code:
    public void actionPerformed(ActionEvent e) {
        txtarea.append(IsmbSnepConnection.received);
    }


Wenn ihr mir behilflich sein könntet würde ich mich sehr freuen.

Danke
 
Zuletzt bearbeitet:
Hallo,

in dem Falle wäre es interessant zu wissen, was genau in der "transceive" Methode geschieht, die das Array ja anscheinend konstruiert. Und du kannst, um aus dem Byte-Array ein String machen zu lassen, denke ich auch die Methode "Arrays.toString(reply)" verwenden.

Gruß,
Wolf
 
Bei der Methode Arrays.ToString bekomme ich nur die Byte zahlen zurück.

Hier ist der ganze Code für Transeive:
Code:
private byte[] transceive(byte instr, byte[] payload) throws IsmbSnepException {
            
                if (ch == null) {
                        throw new IsmbSnepException("channel not open");
                }
                int payloadLength = (payload != null) ? payload.length : 0;
                byte[] instruction = { (byte) 0xd4, instr };

                //ACR122 header
                byte[] header = { (byte) 0xff, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                                (byte) (instruction.length + payloadLength) };

                /* construct the command */
                byte[] cmd = Util.appendToByteArray(header, instruction, 0,
                                instruction.length);
                cmd = Util.appendToByteArray(cmd, payload);

                if (debugMode)
                        Util.debugAPDUs(cmd, null);

                try {
                        CommandAPDU c = new CommandAPDU(cmd);
                        ResponseAPDU r = ch.transmit(c);
                        
                        byte[] ra = r.getBytes();

                        if (debugMode)
                                Util.debugAPDUs(null, ra);

                        /* check whether APDU command was accepted by the Controller */
                        if (r.getSW1() == 0x63 && r.getSW2() == 0x27) {
                                throw new CardException(
                                                "wrong checksum from contactless response");
                        } else if (r.getSW1() == 0x63 && r.getSW2() == 0x7f) {
                                throw new CardException("wrong PN53x command");
                        } else if (r.getSW1() != 0x90 && r.getSW2() != 0x00) {
                                throw new CardException("unknown error");
                        }
                        return Util.subByteArray(ra, 2, ra.length - 4);
                } catch (CardException e) {
                        throw new IsmbSnepException("problem with transmitting data");
                }
        }
        
        public void receivingProcedureEnhanced() throws UnsupportedEncodingException{
        	//System.out.println("Called Procedure to Receive data .. INITIATOR MODE");
        	try {
        		byte[] initiatorPayload = { (byte) 0x01, (byte) 0x02,
        				(byte) 0x04,
        				(byte) 0x46, 
        				(byte) 0x66, (byte) 0x6D, (byte) 0x01, (byte) 0x01, (byte) 0x10,        				      				        				  
						(byte) 0x03, (byte) 0x02, (byte) 0x00,
						(byte) 0x01, (byte) 0x04, (byte) 0x01, (byte) 0x96        		
        		};
        	        		
				transceive(IN_JUMP_FOR_DEP, initiatorPayload);        		
        	} catch (IsmbSnepException e) {e.printStackTrace();}
        	//System.out.println("IN JUMP FOR DEP done..");
        	         
        	
        	//This loop is necessary because I don't exactly know when the other device (e.g. a Smartphone) will decide to push data
        	//in particular it will happen that it will send an LLCP packet containing a connection request (PTYPE=0100)
        	//so I wait for 0x11 0x20 
			while(true){
		        try {
					Thread.sleep(200);
				} catch (InterruptedException e1) {e1.printStackTrace();}
		        	
		        try {
		        
		        	byte[] targetConnect = {(byte)0x01,(byte)0x00, (byte) 0x00 }; 
		        	byte[] reply=transceive(IN_DATA_EXCHANGE, targetConnect);
		        		       		        	
		        	if (((byte)reply[1]==(byte)0x11)&&((byte)reply[2]==(byte)0x20)){ 
						//System.out.println("Connection Request Received");
							break;
						}	
		        } catch (IsmbSnepException e) {e.printStackTrace();}		        
		    }
		 	 try {
					Thread.sleep(200);
				} catch (InterruptedException e1) {e1.printStackTrace();}		
			
			try {	
					byte[] targetConnect = {(byte)0x01,(byte)0x81, (byte) 0x84 };
		        	transceive(IN_DATA_EXCHANGE, targetConnect);
				} catch (IsmbSnepException e) {e.printStackTrace();}
						
				
			while(true){
				try {
					Thread.sleep(200);
				} catch (InterruptedException e1) {e1.printStackTrace();}
			        	
			        try {
			        
			        	byte[] targetConnect = {(byte)0x01,(byte)0x00, (byte) 0x00 };
			        	byte[] reply=transceive(IN_DATA_EXCHANGE, targetConnect);
			        
			        	System.out.println(reply.length + " ");
			        	
			        	if ((byte)reply.length>7){
							System.out.println("Something received:"+ Util.openFileToString(reply));
							received = Arrays.toString(reply);
                                                        //String value = new String(reply, "UTF-8");
                                                        //System.out.println("Something received:"+value);
                                                        break;
							}	
			        } catch (IsmbSnepException e) {e.printStackTrace();}
				}			
       
			
			
			try {	
				byte[] targetConnect = {(byte)0x01,(byte)0x83, (byte) 0x04, (byte) 0x01,
						(byte) 0x10, (byte) 0x81,
						(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00 };
	        	transceive(IN_DATA_EXCHANGE, targetConnect);	        	
			} catch (IsmbSnepException e) {e.printStackTrace();}	
			
        }        
}
 
Schonmal probiert mit:
Code:
String meinString = new String(meinArray);
Falls das auch nicht funktioniert, kann sein dass das Byte-array in einer anderen Kodierung kodiert ist, dann müsstest du diese rausfinden und folgendermaßen schreiben:
Code:
String meinString = new String(meinArray, meineKodierung);
 

Neue Beiträge

Zurück