2017-04-25 2 views
1

J'ai un problème lors de la copie d'une chaîne contenant le caractère hexadécimal 0x00 dans le Presse-papiers et je le colle dans une application externe (éditeur hexadécimal, bloc-notes ++, etc.). Il termine la chaîne à l'octet 0x00.Copie d'une chaîne contenant hexadécimal 0x00 dans le Presse-papiers

Lors de la copie d'une chaîne contenant 0x01 0x02 0x03 0x00 0x01 0x01 0x02 Elle ne colle que 0x01 0x02 0x03 dans les applications externes.

interne (dans le même programme java au moins), il est très bien, et je reçois toute la chaîne arrière, par exemple:

copying string 
Str length = 7 
Str hex = 01 02 03 00 01 02 03 
----- 
Pasting string 
Str length = 7 
Str hex = 01 02 03 00 01 02 03 

Mais après l'exécution de cela, coller le presse-papier vers une application externe ne copie les caractères jusqu'à 0x00. Est-ce que quelqu'un a une idée de comment résoudre ce problème?

source:

import java.awt.Toolkit; 
import java.awt.datatransfer.Clipboard; 
import java.awt.datatransfer.DataFlavor; 
import java.awt.datatransfer.StringSelection; 

public class Test 
{ 
public static void main(String[] args) 
{ 
    String strToCopy = new String(new byte[] {0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03}); 

    System.out.println("copying string"); 
    printStringDetails(strToCopy); 
    copyStringToClipboard(strToCopy); 

    System.out.println("-----"); 

    System.out.println("Pasting string"); 
    String pastedString = getStringFromClipboard(); 
    printStringDetails(pastedString); 
} 

public static void printStringDetails(String string) 
{ 

    System.out.println(String.format("Str length = %d", string.length())); 
    System.out.println(String.format("Str hex = %s", bytesToHex(string.getBytes()))); 
} 

public static void copyStringToClipboard(String strToCopy) 
{ 
    StringSelection stringSelection = new StringSelection(strToCopy); 
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 
    clipboard.setContents(stringSelection, null); 
} 

public static String getStringFromClipboard() 
{ 
    String returnedString = ""; 
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 
    try 
    { 
     returnedString = (String)clipboard.getData(DataFlavor.stringFlavor); 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 

    return returnedString; 
} 

/* 
* Converts bytes array to hex string 
*/ 
public static String bytesToHex(byte[] byteArray) 
{ 
    StringBuilder sb = new StringBuilder(); 
    for(byte b : byteArray) 
    { 
     sb.append(String.format("%02X", b)); 
     sb.append(" "); 
    } 
    return sb.toString(); 
} 
} 

Répondre

0

Dans mon ordinateur portable (OS X), tout va bien si je coller à la chaîne VIM.

enter image description here

Voici l'info hexagonale:

enter image description here

Je pense que le problème dépend de la mise en œuvre de l'application externe. S'il peut gérer un 0x00, il recevra la chaîne entière.

+0

Je vais essayer sur d'autres plateformes. J'ai "manuellement" copié la chaîne d'un programme C++ à Notepad ++ et cela fonctionne, mais pas de Java à Notepad ++, donc je pense que c'est un problème de Java, mais pourrait être faux – Wassa