Jump to content

melns00

Members
  • Posts

    1
  • Joined

  • Last visited

melns00's Achievements

Newbie

Newbie (1/14)

  • First Post Rare
  • Week One Done
  • One Month Later
  • One Year In

Recent Badges

0

Reputation

  1. I am using this code to store serialized String objects: public String serialize(Object str) { java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(); try { java.io.ObjectOutputStream objectOut = new java.io.ObjectOutputStream(baos); objectOut.writeObject(str); } catch(Exception e) { } byte[] buffer = baos.toByteArray(); return byteToHex(buffer); } public String byteToHex(byte[] inBytes) { return byteToHex(inBytes,0,inBytes.length); } public String byteToHex(byte[] inBytes,int startIndex,int endIndex) { byte newByte = 0x00; int i,hexIndex; String hexChars = "0123456789ABCDEF"; StringBuffer outBuffer = new StringBuffer(endIndex - startIndex); if ( inBytes == null || endIndex <= startIndex ) return (String)null; for ( i = startIndex; i < endIndex; i++) { /* * Each Hexadecimal character represents 4 bits and each element of * the byte array represents 8 bits. First strip off the left 4 * bits, shift to the least significant (right) portion of a new * byte, then mask the upper portion to allow proper conversion to an * integer between 0 and 15. This value can be used as the index into * the hexadecimal character string. */ newByte = (byte)(inBytes & 0xF0); newByte = (byte)(newByte >>> 4); newByte = (byte)(newByte & 0x0F); hexIndex = (int)newByte; outBuffer.append(hexChars.substring(hexIndex,hexIndex + 1)); /* * Now strip off the right 4 bits, shift and convert to an integer * between 0 and 15. */ newByte = (byte)(inBytes & 0x0F); hexIndex = (int)newByte; outBuffer.append(hexChars.substring(hexIndex,hexIndex + 1)); } return outBuffer.toString(); }
×
×
  • Create New...