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

byte array question

Scott A Burch
Ranch Hand

Joined: Jan 04, 2011
Posts: 50
How can I put the output of this code into a byte array ?



This code converts the three items in spinnerStr into three ASCII decimals. I need to put those three decimals into a byte array that I can append to a second array.

thank you
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
String has a toByteArray() method. Check out the API.
Scott A Burch
Ranch Hand

Joined: Jan 04, 2011
Posts: 50
"j" is not a string, is it? If I try to use it like this, I get " int cannot be dereferenced"

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

spinnerStr is though.
Scott A Burch
Ranch Hand

Joined: Jan 04, 2011
Posts: 50
I need to convert spinnerStr into ASCII decimals first, then put the decimals into a byte array. Using spinnerStr.getBytes() directly gives me a very different result than what I am looking for ...

So somehow I need to convert int j into a byte array.

thanks!
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Scott A Burch wrote:I need to convert spinnerStr into ASCII decimals first, then put the decimals into a byte array. Using spinnerStr.getBytes() directly gives me a very different result than what I am looking for ...

So somehow I need to convert int j into a byte array.

thanks!


That doesn't make a ton of sense. What do you mean by converting an int into a byte array? What kinds of numbers are you expecting, and what do you want them to be when you convert them? For example, what would the "converted" value be of each of these numbers be:

0
17
-5
-9999
9999
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

Scott A Burch wrote:I need to convert spinnerStr into ASCII decimals first, then put the decimals into a byte array. Using spinnerStr.getBytes() directly gives me a very different result than what I am looking for ...

What exactly is the difference between what you're looking for and what spinnerStr.getBytes() returns? Can you explain it with an example?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I think it would help if we knew what you meant by "an ASCII decimal".
Scott A Burch
Ranch Hand

Joined: Jan 04, 2011
Posts: 50
Assume that the first spinner is set to"2" and the second spinneris set to "2" also. The code below returns "!22 "




With the spinners again each set at "2", the following code returns ASCII values of "33 50 50". These are values I have been calling "decimal", rightly or wrongly. But these are the values that I need to plug into a byte array.


Scott A Burch
Ranch Hand

Joined: Jan 04, 2011
Posts: 50
And I should add that I need to make int j into a byte array because I need to copy that array into a second array that will then make a complete command outputstream that I will send out of a serial port.

thanks!
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Again, you haven't told us what you mean by converting an int into a byte array. Some examples of input ints and desired output byte arrays would help.
Scott A Burch
Ranch Hand

Joined: Jan 04, 2011
Posts: 50
The output of int j should be captured and made to look like this :

Scott A Burch
Ranch Hand

Joined: Jan 04, 2011
Posts: 50
I may have found a solution. I have compiled and run this:



I get an "Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException" at the point of the second System.arraycopy line [ line 18 ]. I believe the first byte array is 6 bytes long, so I think the position where the second array copies into bCombined is 7.

Any ideas? Thanks for the help so far ...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

There is some misunderstanding here. String.getBytes() does exactly the same as your manual looping through the string and getting the characters one by one. Have a look at this program:

Output:

[33, 50, 50]
[33, 50, 50]


The same thing.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

Scott A Burch wrote:I get an "Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException" at the point of the second System.arraycopy line [ line 18 ]. I believe the first byte array is 6 bytes long, so I think the position where the second array copies into bCombined is 7.

Note that array indices are 0-based. The 7th element has index 6. So the position where the second array should be copied has index 6, not 7.
Scott A Burch
Ranch Hand

Joined: Jan 04, 2011
Posts: 50
Thanks for all the help --- it helped me sort out the byte question and it works great. It's the array copy that gives me an :

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException

on line 12. I don't understand how it's out of bounds when the length variable ought to give it some flexibility. Thanks again!

Code is below:

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14153
    
  18

In line 12, as the last argument you'll want b2.length instead of bCombined.length.
Scott A Burch
Ranch Hand

Joined: Jan 04, 2011
Posts: 50
I re-read the Java tutorial page on Arrays and the paragraph on array copy. I realized what the mistake was and fixed it and it all works properly.

Thanks to all for your patience and help, particularly to Jesper for the comparative illustration above.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: byte array question