• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

How to convert from byte[] to string in form of 0x000000000

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

This is my first post, so if i correct a mistake (like putting this where it is not supposed to be) sorry beforehand.

Im currently developing a service in a service bus that recieves mails and serialices attachments. The insertion to the database is done by another ESB service, which receives the query as part of an XML.

the attachment should be serialized as varbinary(max) in SQL server 2005, but i'm having trouble to construct the appropiate query.

I've tried to do it using the toString() method of the array. Problem is that i can only do it like that if byte[].length < 128. I also tried to specify a big value in the form of 0x36549641616851646, and that actually works. What I'm now missing is the transformation part from byte[] to a string in that format.

What can i do?

thanks!
Efrain
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Efrain Morales wrote:I've tried to do it using the toString() method of the array. Problem is that i can only do it like that if byte[].length < 128.



This can't work. The toString() method of the byte array merely returns a string that it is an array -- and an identity hash. The values in the array has no effect on the toString() method.

Efrain Morales wrote: I also tried to specify a big value in the form of 0x36549641616851646, and that actually works. What I'm now missing is the transformation part from byte[] to a string in that format.



I have no idea what you mean by this. Please elaborate.

Henry
 
Sheriff
Posts: 22696
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Efrain Morales wrote:the attachment should be serialized as varbinary(max) in SQL server 2005, but i'm having trouble to construct the appropiate query.
...
What can i do?


Use PreparedStatement and one of its setXXX methods. I think there is one called setBytes.
 
Efrain Morales
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Henry

What i meant with 0x00000 is that i can execute the query like this: sp_SaveMail 'Sender','Receiver','attachmentName',0x9342743 where the 0x9342743 represents the varbinary value of the attachment, so what i wanted to know is that if there was a way to represent the value stored in the byte[] using that format



@Rob Prime

I thought of it, but due to company policies I do not have access to database passwords in production, so I cannot create a connection therefore I cannnot create the prepared statement either. I just did some research but i found no way to create the prepared statement without creating a connection first.
 
Rob Spoor
Sheriff
Posts: 22696
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you cannot create a connection, how can you insert the data in your database at all?

Anyhow, if you want to convert this, check out Integer.toHexString. You will need to call this for all your bytes, and not forget to add a leading 0 if the returned String would be just one character.
 
Efrain Morales
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Efrain Morales wrote:
The insertion to the database is done by another ESB service, which receives the query as part of an XML.


Meaning that I do not have direct access to the database, but I can send the query via JMS and it gets executed.

I've tried your recommendation but apparently it will only work for arrays of length of 128 or less. Anything bigger results in the following error:

Msg 103, Level 15, State 5, Line 0
The number that starts with '37806870454946511037868584831052324832111981061060603247841211121013247731101021111047801141111001179910111432407079803248465048' is too long. Maximum length is 128.

*Edit*
It will only work for arrays of 64 bytes, since each byte is 2 chars long

Any ideas?
 
Rob Spoor
Sheriff
Posts: 22696
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) split the String into pieces of 2 characters
2) create a byte[] of the same length as the number of pieces
3) parse each piece into an array element
 
Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic