File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes String to binary conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String to binary conversion " Watch "String to binary conversion " New topic
Author

String to binary conversion

Rosu Thomas
Greenhorn

Joined: Jun 19, 2012
Posts: 17
hi ,
i am getting a string "0x01" from textbox of my jsp page.i want to store it in my table as binary.how to do this using preparedstatement of mysql. Also like this i need to store binary of variable length in my table.please give me a solution.
thanks in advance
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

Hi Rosu,

There are various ways to store binary data in DB tables:

Convert the data to binary, and then encode it to Base64 encoding. Apache provides a ready-made implementation for Base64 encoding-decoding.
Pros: Base64 is pure char format. So this data can be stored in any varchar column. Also, this is easier to manage than blob.
Cons: When a byte-buffer is converted to Base64 format, it's size is increased (typically by 33%) - and varchar column generally has limit of 4000 chars.

Store data as blob.
Pros: Data can be directly stored as binary. No need to do any encoding-decoding. Also, large objects can be stored (e.g. zipped files etc.)
Cons: If original data is not in byte format, then char-to-byte conversion has to be done manually - which is more error prone than Base64 encoding-decoding.

There is a third way of doing it via RAW datatype (which is supported on Oracle) - however, I'm not sure if that is supported on MySql.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Rosu Thomas
Greenhorn

Joined: Jun 19, 2012
Posts: 17
HI,
thanks for responding to my query.actually i don't want to convert my data to hex.i just want to store it in my column of type BINARY(3).
for example,i am getting "23" from my textbox..and i want to store it in my key-id column of type BINARY(3) as 23 itself..i am using preparedstatement for inserting it in my table.
please revert ASAP.
thanks in advance
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

Now I'm confused. If your column is binary, then 23 will be stored in binary format only(and not in decimal/string format).

If you want to store 23 in decimal/string format, you may need to change column type accordingly.

If, due to some reason, changing column type is not possible, then I guess conversion (either manual or via utility - as mentioned in my previous post) is the only way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String to binary conversion
 
Similar Threads
Upload Picture - Path or Binary?
Compression Algorithm
Binary to string conversion
Convert decimal value to binary
Binary in java - guide me