• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

byte array question

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String has a toByteArray() method. Check out the API.
 
Scott A Burch
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"j" is not a string, is it? If I try to use it like this, I get " int cannot be dereferenced"

 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
spinnerStr is though.
 
Scott A Burch
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it would help if we knew what you meant by "an ASCII decimal".
 
Scott A Burch
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output of int j should be captured and made to look like this :

 
Scott A Burch
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In line 12, as the last argument you'll want b2.length instead of bCombined.length.
 
Scott A Burch
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic