Results 1 to 13 of 13
Thread: Byte Array
- 05-01-2007, 09:04 PM #1
Member
- Join Date
- Apr 2007
- Location
- Pennsylvania,USA
- Posts
- 45
- Rep Power
- 0
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, 09:09 PM #2
Member
- Join Date
- Apr 2007
- Location
- USA
- Posts
- 50
- Rep Power
- 0
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, 07:19 AM #3
Member
- Join Date
- Mar 2007
- Posts
- 41
- Rep Power
- 0
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, 09:16 PM #4
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, 09:40 PM #5
Free open source code for this at my website, pfarrell.com
look in the /java subdirectory
Technical Notes
- 08-28-2008, 06:45 PM #6
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, 07:44 PM #7
How did you convert the byte[] to String? That will determine how toI have the right value but in the form of a String. .
convert the value I have in my String to bytes
- 08-28-2008, 09:29 PM #8
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.}Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 08-29-2008, 12:28 PM #9
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/DecoderLast edited by ProjectKaiser; 08-29-2008 at 02:59 PM.
- 11-16-2008, 11:15 AM #10
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
heLLo,,!!
cAn u pLeAse give mE tHE cOde of tHis oUtpUt?
using aRRay
@
@@
@ @
@@@@
@ @
@@ @@
@ @ @ @
@@@@@@@@
@ @
@@ @@
@ @ @ @
@@@@ @@@@
@ @ @ @
@@ @@ @@ @@
@ @ @ @ @ @ @ @
@@@@@@@@@@@@@@@@
@ @
@@ @@
@ @ @ @
@@@@ @@@@
@ @ @ @
@@ @@ @@ @@
@ @ @ @ @ @ @ @
@@@@@@@@ @@@@@@@@
@ @ @ @
@@ @@ @@ @@
@ @ @ @ @ @ @ @
@@@@ @@@@ @@@@ @@@@
@ @ @ @ @ @ @ @
@@ @@ @@ @@ @@ @@ @@ @@
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- 11-16-2008, 11:19 AM #11
Member
- Join Date
- Nov 2008
- Posts
- 5
- Rep Power
- 0
arrays
heLLo,,!!
cAn u pLeAse give mE tHE cOde of tHis oUtpUt using two dimensional array?
thAnks a lot
@
@@
@ @
@@@@
@ @
@@ @@
@ @ @ @
@@@@@@@@
@ @
@@ @@
@ @ @ @
@@@@ @@@@
@ @ @ @
@@ @@ @@ @@
@ @ @ @ @ @ @ @
@@@@@@@@@@@@@@@@
@ @
@@ @@
@ @ @ @
@@@@ @@@@
@ @ @ @
@@ @@ @@ @@
@ @ @ @ @ @ @ @
@@@@@@@@ @@@@@@@@
@ @ @ @
@@ @@ @@ @@
@ @ @ @ @ @ @ @
@@@@ @@@@ @@@@ @@@@
@ @ @ @ @ @ @ @
@@ @@ @@ @@ @@ @@ @@ @@
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- 12-12-2008, 10:40 AM #12
Member
- Join Date
- Dec 2008
- Posts
- 5
- Rep Power
- 0
Additional query with the same problem
When I am running the code as follows:
BufferedReader userInput = new BufferedReader (new InputStreamReader(System.in));
System.out.println("Enter String: ");
String input = userInput.readLine();
System.out.println("UserInput:"+input);
byte[] sha1hash = input.getBytes();
System.out.println("ByteFormat:"+sha1hash);
String t = new String( sha1hash);
System.out.println("StringFormat:"+t);
for (int i = 0; i < sha1hash.length; i++)
System.out.println("ByteArray["+i+"]:"+sha1hash[i]);
I am getting the output as follows:
Enter String:
23
UserInput:23
ByteFormat:[B@10b62c9
StringFormat:23
ByteArray[0]:50
ByteArray[1]:51
I am unable to understand that when I am printing each element of the byte array where from it is getting those values.
- 01-15-2009, 03:31 AM #13
You're not doing a hash, it is something else.....
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Similar Threads
-
Byte Values
By javaplus in forum New To JavaReplies: 1Last Post: 06-23-2008, 12:08 AM -
using Byte arrays
By mew in forum New To JavaReplies: 2Last Post: 01-30-2008, 03:54 AM -
BufferedImage to Byte
By Java Tip in forum Java TipReplies: 0Last Post: 01-22-2008, 08:17 PM -
Reading/Writing a File using byte array
By Java Tip in forum Java TipReplies: 0Last Post: 01-16-2008, 10:41 AM -
int to byte
By ravian in forum New To JavaReplies: 1Last Post: 01-13-2008, 07:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks