jQuery in Action, 3rd edition
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

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ascii decimal conversion problem" Watch "ascii decimal conversion problem" New topic

ascii decimal conversion problem

Scott A Burch
Ranch Hand

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

Joined: Sep 20, 2010
Posts: 4087

This looks like the means towards a goal. What is your goal?

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Scott A Burch
Ranch Hand

Joined: Jan 04, 2011
Posts: 54
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: 54
Here's the block in question:

Paul Clapham

Joined: Oct 14, 2005
Posts: 19747

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

Joined: Oct 27, 2005
Posts: 20131

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.

How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14996

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: 54
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.
I agree. Here's the link: http://aspose.com/file-tools
subject: ascii decimal conversion problem
jQuery in Action, 3rd edition