How to convert from hexadecimal to Base64?

Asked

Viewed 676 times

0

I’m getting a hexadecimal image from the database and have to convert it to base 64, only I don’t know how to do that. I am currently setting it as string and returning to json:

Model

 private String image;

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

DAO

public List<ProductModel> getProducts() {

    String sql = "SELECT " + this.defaultSelect + " FROM produto";
    List<ProductModel> result = new ArrayList<>();

    PreparedStatement pst = MyConnection.getPreparedStatement(sql);
    try {

        ResultSet res = pst.executeQuery();
        while (res.next()) {
            ProductModel item = new ProductModel();
            item.setImage(res.getString("imagem"));
            result.add(item);
        }

    } catch (SQLException ex) {
        Logger.getLogger(ProductDAO.class.getName()).log(Level.SEVERE, null, ex);


    return result;

}

The json I’m sending is this:

    [
  {
    "id": 1,
    "name": "Glicopan Pet",
    "price": "29.9000",
    "description": "Glicopan Pet® auxilia na suplementação de aminoácidos e vitaminas do complexo B em caninos, felinos, aves, mustelídeos, répteis e roedores, contribuindo com a melhora geral de animais com estado nutricional inadequado. Glicopan Pet® também estimula o apetite, além de ser um energético indicado para o preparo de animais de exposição e em treinamento.",
    "discountPromotion": "0.00",
    "activeProduct": 1,
    "minimumStockQuantity": 20,
    "categoryId": 5,
    "image": "FFD8FFE000104A46494600010101006000600000FFDB0043000402030303020403030304040404050906050505050B080806090D0B0D0D0D0B0C0C0E1014110E0F130F0C0C1218121315161717170E11191B19161A14161716FFDB0043010404040505050A06060A160F0C0F1616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616161616FFC000110801EA01EA03012200021101031101FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A161718191A25262728292A3435363738393A434445464748494A535455565758595A636465666768696A737475767778797A838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FAFFC4001F0100030101010101010101010000000000000102030405060708090A0BFFC400B51100020102040403040705040400010277000102031104052131061241510761711322328108144291A1B1C109233352F0156272D10A162434E125F11718191A262728292A35363738393A434445464748494A535455565758595A636465666768696A737475767778797A82838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE2E3E4E5E6E7E8E9EAF2F3F4F5F6F7F8F9FAFFDA000C03010002110311003F00FBFA8A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A28A0028A2B2FC49E20D2742B7F3351BA58C9195897E691FE8A2A6538C15E4EC86936EC8D4A2BC7FC5DF11B54D44B5BE96A6C2DD8E0B8399987D7F87F0E7DEBD42C6C6DA2B386348C8548C01F3127A7A939AE7C3E2E9D794943A173A72824DF53428AABF668BB6F1F47347D9D4749251F490D75D8CAE5AA3359F369CB2485FED97C84F64B8603F2E94CFECD703E5D53501F5955BF9AD4EA334E8ACD16578A3E4D62EBFE0491B7FECB47D9752EDAC3FF00C0ADD3FA0146BD80D2A2B37C9D5870BA9427FDEB5FF06A08D6870B7162DEED030FE4D46BD80D2A2B3776B4A3FE5C18FF00C0D7FC68F3F585EB6B64DF49D87FECB45FC82E8D2A2B345E6A83AE9909FA5DFF008AD0DA85F8EBA4B1FF0076E53FAE28B81A54566FF69CE3EF69575F83C67FF66A3FB58E79D3AF87BEC53FC9A8B81A545670D5E2070D697CBF5B66FE94BFDB3643EF0B95FADAC9FF00C4D1740685159DFDB9A677B92BFEF44E3F98A72EB5A

How can I return the image on base 64 to display on android?

2 answers

1

Take a look at Commons Codec :

byte[] decodedHex = Hex.decodeHex(hex);
byte[] encodedHexB64 = Base64.codeBase64(decodedHex);

0

From to use the library Guava

byte[] bytes = BaseEncoding.base64().decode(hex);
String base64 = BaseEncoding.base64().encode(bytes);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.