Serializable Objects are stored as Hex, so:

public abstract void setNameField(Serializable name);
public abstract Serializable getNameField();

public void setName(String name) {
        setNameField(name);
}

public String getName() {
        return (Serializable) getNameField();
}

Since most RDBMS have a limit(a very low limit) on the size of VARCHAR
types, it's not unusual for some apps to store big strings (> 4KB or >
64KB depending on the implementation) on CLOB/BLOB fields. I have to
code like this to store large XML snippets, etc.

Converting a Hex formatted String into a byte [] should be straight
forward:

private static final String HEXINDEX  =
        "0123456789abcdef0123456789ABCDEF";

public static byte[] hexToByte(String s) throws SQLException {

        int  l      = s.length() / 2;
        byte data[] = new byte[l];
        int  j      = 0;

        for (int i = 0; i < l; i++) {
            char c = s.charAt(j++);
            int  n, b;

            n = HEXINDEX.indexOf(c);

            if (n == -1) {
                throw Trace.error(
                    Trace.INVALID_ESCAPE,
                    "hexadecimal string contains non hex character");
            }

            b       = (n & 0xf) << 4;
            c       = s.charAt(j++);
            n       = HEXINDEX.indexOf(c);
            b       += (n & 0xf);
            data[i] = (byte) b;
        }

        return data;
    }

(I stole this code from hsqldb. Check out sourceforge for hsqldb if you
want a superfast DB with a small footprint)

Then use the various java.lang.String constructors to apply the correct
encoding. Java is usually smart enough to convert a byte [] into a
properly encoded String without any extra information.

Juan Pablo Lorandi
Chief Software Architect
Code Foundry Ltd.
[EMAIL PROTECTED]

Barberstown, Straffan, Co. Kildare, Ireland.
Tel: +353-1-6012050  Fax: +353-1-6012051
Mobile: +353-86-2157900
www.codefoundry.com


> -----Original Message-----
> From: A mailing list for Enterprise JavaBeans development
> [mailto:EJB-INTEREST@;JAVA.SUN.COM] On Behalf Of May Charles N
> Sent: Monday, November 11, 2002 6:54 PM
> To: [EMAIL PROTECTED]
> Subject: Re: Converting a String containing Hexadecimal
> values to proper format String
>
>
> I don't know why you're getting Strings that represent the
> Unicode byte representations of your strings. My guess is
> that whatever is populating the database is pumping the data
> in incorrectly.
>
> Meanwhile, here's a workaround.
>
> String s = "0x736D616C6C6170754069782E6E6574636F6D2E636F6D";
>
> //account for all pairs except "0x" header s
> byte[] b = new byte[Math.max(0, s.length()/2 - 1)];
>
> //translate bytes from character pairs, starting after header
> "0x" for (int i=2, j=0; i < s.length(); i+=2, j++) {
>     b[j] = Byte.parseByte( s.substring(i,i+2), 16 );
> }
> String translated = new String(b);
>
> System.out.println(translated);
>
> ==============================================================
> =============
> To unsubscribe, send email to [EMAIL PROTECTED] and
> include in the body of the message "signoff EJB-INTEREST".
> For general help, send email to [EMAIL PROTECTED] and
> include in the body of the message "help".
>
>

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to