• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ascii decimal conversion problem

 
Scott A Burch
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a way to take the output of a JSpinner with a custom value list - 00, 01, 02, etc, to 10 - and convert that to an ASCII decimal sequence. For example, "00" would like "48 48" and so on. I don't know how to do that conversion - a hint or a link would be helpful.

The second part of the problem comes when those new values have to be placed into the second and third slots of the example sequence below:



byte[] arr = {(byte)33, (byte)48, (byte)48, (byte)66, (byte)67, (byte)78, (byte)49, (byte)13};



Any help or advice is always appreciated. Thank you!
 
Stephan van Hulst
Bartender
Pie
Posts: 5885
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks like the means towards a goal. What is your goal?
 
Scott A Burch
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The goal is to slide that array through a serial port via an outputstream to control an external device. My preliminary model works only when the array is "hard coded". I have to find a way for it to be more flexible so that a user can select a network of devices to control.
 
Scott A Burch
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the block in question:


 
Paul Clapham
Sheriff
Posts: 21111
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also missing why solving that problem reduces to the question you posted. If it were me I would just put a suitable list of objects into the spinner's model and just use data from those objects. Or something like that... I didn't read your requirement all that closely.
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two smaller problems at hand here:
1) getting the JSpinner to show values from 00 to 10.
2) converting the current value to two digits.


For problem 1 you can use a SpinnerListModel if your inputs are limited in number. Otherwise, use a SpinnerNumberModel and a JSpinner.NumberEditor to display the value using two digits, then get the value back and use a NumberFormat to format it to two digits.

For problem 2 you can, with a two character String, simply get the two chars (charAt) and convert them to int to get the ASCII value - as long as the chars are digits of course.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't double-click the Code button. Make sure your code is between a [code] begin tag and [/code] end tag.
 
Scott A Burch
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the code tag tip.

My JSpinner actually works fine right now. I am using the ListModel and just populated the list with the items "01" "02" etc, up to "10". Thanks for the info on JSpinner NumberEditor; I was introduced to spinners on the fly for this project.

charAt should do the trick for decimal conversion.

I'll start a new thread for the next problem I have ... ha!

thanks to all again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic