• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String to binary conversion

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rosu Thomas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic