NFe - converter digVal(byte[]) em uma String [RESOLVED]

5 respostas
otOtavio

Galera, estou querendo imprimir em tela o digVal do TRetConsReciNFe, o tipo dele é em byte[]…

quando a receita me retorna o xml de resposta. Eu o converto utilizando o seguinte metodo

public class ParseXML<T> {  
      
    public Object parseXML(String xml, String instance){  
        try {  
            JAXBContext context = JAXBContext.newInstance(instance);  
            Unmarshaller unmarshaller = context.createUnmarshaller();  
            JAXBElement<T> element = (JAXBElement<T>) unmarshaller.unmarshal(new StringReader(xml));  
              
            return element.getValue();  
        } catch (JAXBException ex) {  
            Logger.getLogger(ParseXML.class.getName()).log(Level.SEVERE, null, ex);  
            JOptionPane.showMessageDialog(null, "Falha na Converção do XML");  
  
            return null;  
        }  
    }  
      
}

eu converto o retorno para o tipo TRetConsReciNFe.

e para imprimir em tela estou fazendo o seguinte,

TRetConsReciNFe ret = (TRetConsReciNFe) new ParseXML<TRetConsReciNFe>().parseXML(envio.getXml_retorno(), "br.inf.portalfiscal.nfe"); fDValue.setText(new String((byte[])ret.getProtNFe().get(0).getInfProt().getDigVal()));

mas ele aparece tudo bugado… olha…

x&#65533;&#65533;&#65533;@&#65533;&#65533;K&#65533;&#65533;-&#65533;&#65533;

se alguem souber como arrumar… pois tambem preciso salvar este valor em banco

grato otávio

5 Respostas

otOtavio

ninguem sabe ? =\

E

Você tem de converter esses bytes para uma string hexadecimal, não? new String não vai fazer o que você quer.

otOtavio

e como eu faria isso ?

tentei assim mas nao deu certo olha

static final String HEXES = "0123456789ABCDEF"; public static String getHex( byte [] raw ) { if ( raw == null ) { return null; } final StringBuilder hex = new StringBuilder( 2 * raw.length ); for ( final byte b : raw ) { hex.append(HEXES.charAt((b & 0xF0) >> 4)) .append(HEXES.charAt((b & 0x0F))); } return hex.toString(); }

acho que se eu mapear o xml e pegar o value da tag fica mais facil do que converter o xml

otOtavio

bom.... resolvi assim =\\

public String getDigVal(String xml) throws Exception {
        String retornoValorTag = new String();
        try {
            String xmlstring = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + xml;
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = factory.newDocumentBuilder();
            InputSource inStream = new InputSource();
            inStream.setCharacterStream(new StringReader(xmlstring));
            Document doc1 = db.parse(inStream);
            doc1.getDocumentElement().normalize();
            Node node = doc1.getElementsByTagName("digVal").item(0);
            
            retornoValorTag = node.getChildNodes().item(0).getNodeValue();
        } catch (Exception e) {
            throw new Exception(e);
            
        }
        return retornoValorTag;
    }

não queria dessa maneira mas =\\\ estagio ta ae neh ausdhausdh

R

achei o tópico pesquisando sobre C#, talvez no java tenha algo parecido.

utilizei Convert.ToBase64String(byte[])

e funcionou.

Criado 29 de setembro de 2011
Ultima resposta 10 de nov. de 2015
Respostas 5
Participantes 3