GeeCON Prague 2014*
The moose likes Java in General and the fly likes ascii decimal conversion problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "ascii decimal conversion problem" Watch "ascii decimal conversion problem" New topic
Author

ascii decimal conversion problem

Scott A Burch
Ranch Hand

Joined: Jan 04, 2011
Posts: 50
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

Joined: Sep 20, 2010
Posts: 3647
    
  16

This looks like the means towards a goal. What is your goal?
Scott A Burch
Ranch Hand

Joined: Jan 04, 2011
Posts: 50
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

Joined: Jan 04, 2011
Posts: 50
Here's the block in question:


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

Don't double-click the Code button. Make sure your code is between a [code] begin tag and [/code] end tag.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Scott A Burch
Ranch Hand

Joined: Jan 04, 2011
Posts: 50
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.
 
GeeCON Prague 2014
 
subject: ascii decimal conversion problem