0

I got a class "Transaction" with Attributes in it like the id(byte[]), sender(byte[]), receiver(byte[]) and amount (double).

public class Transaction implements Serializable
{
    private transient byte[] txId;

    private byte[] sender;

    private byte[] receiver;

    private double amount;

Now I want to display these attributes in a TableView and this is the Method I´m using for:

    @FXML
    void refreshButtonPressed(ActionEvent event) {
        List<Transaction> incomingList = 
        DependencyManager.getAccountStorage().getAccount(publicKey).getIncomingTransactions();
        incomingTransactions = FXCollections.observableList(incomingList);
        idCol.setCellValueFactory(new PropertyValueFactory<Transaction, String>("txId"));
        senderCol.setCellValueFactory(new PropertyValueFactory<Transaction, String>("sender"));
        receiverCol.setCellValueFactory(new PropertyValueFactory<Transaction, String>("receiver"));
        amountCol.setCellValueFactory(new PropertyValueFactory<Transaction, String>("amount"));
        incomingView.setItems(incomingTransactions);
}

Well the good thing is - it works, basically. But i want to convert to byte[] to a String with my own converter i wrote because this standard byte to String conversion is not really readable by the User.

Thats the method im using for the conversion: (Its from bouncycastle).

public static String digestToHex( byte[] digest )
    {
        return Hex.toHexString( digest );
    }

enter image description here

So is there any way to convert the data before it is displayed?

Thriest
  • 44
  • 5
  • 1
    Why are you encoding the bytes to hex instead of decoding the bytes to a string using the appropriate [character encoding](https://www.baeldung.com/java-char-encoding)? – jewelsea Dec 10 '22 at 09:56
  • 3
    You should [avoid PropertyValueFactory](https://stackoverflow.com/questions/72437983/why-should-i-avoid-using-propertyvaluefactory-in-javafx). – jewelsea Dec 10 '22 at 09:59
  • 2
    You can provide your own cell factory or cell value factory to convert the bytes to strings using whatever encoding method you end up deciding to use. Either approach would work in this case. – jewelsea Dec 10 '22 at 10:02
  • 1
    Alternately, in your `getIncomingTransactions` method, you could do the bytes to string conversion there and store the converted string in your `Transaction` class. – jewelsea Dec 10 '22 at 10:26
  • 2
    I'm assuming those byte arrays have binary data and *do not* contain strings (or you could just create strings from them). Since 17 there is `java.util.HexFormat` but even if you're behind that, using a 3rd party library is overkill. For one thing, you have `String.format` – g00se Dec 10 '22 at 11:17
  • [`HexFormat` usage](https://stackoverflow.com/a/69232227/1155209) in case you need that. – jewelsea Dec 10 '22 at 11:59

0 Answers0