|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

05-01-2007, 11:04 PM
|
|
Member
|
|
Join Date: Apr 2007
Location: Pennsylvania,USA
Posts: 46
|
|
|
Byte Array
I have a PublicKey object that I haved called getEncoded() on to get its representation as an array of bytes.
I then want to send these to another client so they can use the array of bytes.
InputStreamReader can only read Strings at a time, so I have the right value but in the form of a String.
I need to convert the value I have in my String to bytes. Example:
I print out the array of bytes on the server side, I get:
[B@c79809
I print out the String read on the receiving end, I get:
[B@c79809
It's the same, so I want that exact value in an array of bytes rather than a String. How do I get that?
|
|

05-01-2007, 11:09 PM
|
|
Member
|
|
Join Date: Apr 2007
Location: USA
Posts: 50
|
|
|
The subclasses of Reader are meant for character streams.
To read bytes, look at BufferedInputStream.
Just a reminder that what you see in "[B@c79809" is a string representation of a byte array; the "[B" part signifies that it's a byte array
(as opposed to, say, an int array), and the "@c79809" part is a representation of the array's address. You may know this already,
but I wanted to make sure you didn't mistake that string for the encoded byte values.
|
|

05-06-2007, 09:19 AM
|
|
Member
|
|
Join Date: Mar 2007
Posts: 41
|
|
|
If the "toString () " method is not implemented we get the values something like this ? So the question is what is the relationship between bytearray and the toString ?
|
|

06-20-2008, 11:16 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 783
|
|
|
Use Base64 encoding, I found one in the libs yesterday and as well have an open source Base64 encoder.
Since we have key material or something, doing toString() and so on is either inefficient, ineffective or insecure for some reason or another. The proof of concept is that any and all pro's use Base64 encoding for transmission of binary - that is what it was designed for and would not have been written unless there was a need for it.
|
|

06-20-2008, 11:40 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Posts: 475
|
|
|
Free open source code for this at my website, pfarrell.com
look in the /java subdirectory
Technical Notes
|
|

08-28-2008, 08:45 PM
|
 |
Member
|
|
Join Date: Aug 2008
Location: Saint-Petersburg, Russia
Posts: 47
|
|
|
I think the following code will give your some figures:
byte b1[] = new byte[]{1, 11, 2, 22, 33, 127};
System.out.println(Arrays.toString(b1));
String s = new String(b1, "utf-8");
//System.out.println(s);
byte b2[] = s.getBytes("utf-8");
System.out.println(Arrays.toString(b2));
|
|

08-28-2008, 09:44 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
I have the right value but in the form of a String. .
How did you convert the byte[] to String? That will determine how to
convert the value I have in my String to bytes
|
|

08-28-2008, 11:29 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 783
|
|
|
base64 v DataOutputStream
There is a missing element here of how it is that original poster is stuck with going through a string, I noticed in ftr's comments in the base64 encoder something to the effect of may not be reversable. As Norm states, it is how opie converts in the first place that matters for getting data back to bin - hex - octets but going through String method seems to introduce indefinite risks surrounding contemporary String ( ususaly viewed as human-readable or at least containing few if any nulls ) vis-a-vis byte / binary data transmission. Those are distinguishable problem domains.
{Norm, I got a couple of 500-800 page Java books back from my bench tech, let me know if you want me to ship them to you as they are not being used by me any more.}
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. .
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
|
|

08-29-2008, 02:28 PM
|
 |
Member
|
|
Join Date: Aug 2008
Location: Saint-Petersburg, Russia
Posts: 47
|
|
I agree that going though String is potentially dangerous, but...There is no Base64 encoder/decoder in the standard Java SDK class library !
There is Sun specific sun.misc.BASE64Encoder, but it is undocumented and generally can not be used.
You can use things like this:
Base64: Public Domain Base64 Encoder/Decoder
Last edited by ProjectKaiser : 08-29-2008 at 04:59 PM.
|
|

11-16-2008, 01:15 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 5
|
|
|
heLLo,,!!
cAn u pLeAse give mE tHE cOde of tHis oUtpUt?
using aRRay
@
@@
@ @
@@@@
@ @
@@ @@
@ @ @ @
@@@@@@@@
@ @
@@ @@
@ @ @ @
@@@@ @@@@
@ @ @ @
@@ @@ @@ @@
@ @ @ @ @ @ @ @
@@@@@@@@@@@@@@@@
@ @
@@ @@
@ @ @ @
@@@@ @@@@
@ @ @ @
@@ @@ @@ @@
@ @ @ @ @ @ @ @
@@@@@@@@ @@@@@@@@
@ @ @ @
@@ @@ @@ @@
@ @ @ @ @ @ @ @
@@@@ @@@@ @@@@ @@@@
@ @ @ @ @ @ @ @
@@ @@ @@ @@ @@ @@ @@ @@
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
|

11-16-2008, 01:19 PM
|
|
Member
|
|
Join Date: Nov 2008
Posts: 5
|
|
|
arrays
heLLo,,!!
cAn u pLeAse give mE tHE cOde of tHis oUtpUt using two dimensional array?
thAnks a lot
@
@@
@ @
@@@@
@ @
@@ @@
@ @ @ @
@@@@@@@@
@ @
@@ @@
@ @ @ @
@@@@ @@@@
@ @ @ @
@@ @@ @@ @@
@ @ @ @ @ @ @ @
@@@@@@@@@@@@@@@@
@ @
@@ @@
@ @ @ @
@@@@ @@@@
@ @ @ @
@@ @@ @@ @@
@ @ @ @ @ @ @ @
@@@@@@@@ @@@@@@@@
@ @ @ @
@@ @@ @@ @@
@ @ @ @ @ @ @ @
@@@@ @@@@ @@@@ @@@@
@ @ @ @ @ @ @ @
@@ @@ @@ @@ @@ @@ @@ @@
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|