This week's book giveaways are in the Refactoring and Agile forums.We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!

# byte array question

Scott A Burch
Ranch Hand
Posts: 58
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
Posts: 151
String has a toByteArray() method. Check out the API.

Scott A Burch
Ranch Hand
Posts: 58
"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
Posts: 5371
52
spinnerStr is though.

Scott A Burch
Ranch Hand
Posts: 58
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
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
Posts: 15207
36
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?

Paul Clapham
Sheriff
Posts: 20750
30
I think it would help if we knew what you meant by "an ASCII decimal".

Scott A Burch
Ranch Hand
Posts: 58
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
Posts: 58
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
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
Posts: 58
The output of int j should be captured and made to look like this :

Scott A Burch
Ranch Hand
Posts: 58
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
Posts: 15207
36
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
Posts: 15207
36
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
Posts: 58
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
Posts: 15207
36
In line 12, as the last argument you'll want b2.length instead of bCombined.length.

Scott A Burch
Ranch Hand
Posts: 58
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.