4 Replies Latest reply: Aug 19, 2013 9:53 AM by rp0428 RSS

    how to convert Object into byte[]?

    shivasha

      Hi,

       

      I stored one object in session and, have to pass into another method which expects byte[].
      How to do this conversion?

       

      Did conversion but getting classcastexection
      java.lang.ClassCastException: java.lang.String cannot be cast to [B

        • 1. Re: how to convert Object into byte[]?
          masijade

          Explain "did conversion".

           

          And what is the "object", and why does the other method expect a byte[]?

           

          What does the other method do?

           

          In an case, you can only serialize the object, i.e. wrap a bytearrayoutputstream in a objectoutputstream and write the object to it.  Not that I can even begin to imagine how this would benefit anything.

          • 2. Re: how to convert Object into byte[]?
            shivasha

             

            Object obj = session.getAttribute("val");

            // i have to pass obj to below method

            public static String decryptText(byte[] userText, PrivateKey key) {

             

            byte[] dectyptedText = null;
            try {
            // get an RSA cipher object and print the provider
            final Cipher cipher = Cipher.getInstance(ALGORITHM);

             

            cipher.init(Cipher.DECRYPT_MODE, key);
            dectyptedText = cipher.doFinal(text);

             

            } catch (Exception ex) {
            ex.printStackTrace();
            }

             

            return new String(dectyptedText);
            }

            • 3. Re: how to convert Object into byte[]?
              jwenting

              ok, and how do you think that Object you get from "val" is going to match the content expected in that byte array?

              Most likely it expects some sort of mapping of a String to a byte, converting the multibyte characters of Java to single byte characters, probably by stripping anything that's not ASCII.

               

              If the Object represents a String, that's not too hard to do. If it doesn't, god only knows what steps you'd have to take as it would depend entirely on the data structure of that object.

              • 4. Re: how to convert Object into byte[]?
                rp0428

                shivasha wrote:

                 

                Hi,

                 

                I stored one object in session and, have to pass into another method which expects byte[].
                How to do this conversion?

                 

                Did conversion but getting classcastexection
                java.lang.ClassCastException: java.lang.String cannot be cast to [B

                What is this line of code doing?

                Object obj = session.getAttribute("val");

                If the underlying datatype 'getAttribute' is returning is a String then just cast 'obj' to a String and use the 'getBytes' method to get a byte array

                byte[] attributeArray = ((String) obj).getBytes();

                See the String Java API

                http://docs.oracle.com/javase/6/docs/api/java/lang/String.html