aspose file tools*
The moose likes Java in General and the fly likes Data Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Data Conversion" Watch "Data Conversion" New topic
Author

Data Conversion

duhit Choudhary
Ranch Hand

Joined: Apr 01, 2012
Posts: 64
Hello all,
I have made a program for a relay IA-3088, in which i have to ON and OFF the different sets relay lights via my program and i think i succeeded in doing that. my program is also able to show the status if the rely lights will manually On and OFF but the problem is the status it is showing for different combinations of ON and OFF lights is hex form and i want to show the result i the binay form like if light 1, 2 and 8 are on & rest are of then status would be 10000011. here is my code with different four classes.

LightController_new.java


LightManager.java


Lights_new.java


Reader.java


please help me in getting the desired output and to find any other flaw in the code.... it will be a great help. thanks a lot in advance
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14337
    
  22

I've added code tags to your post - see UseCodeTags on how to do that yourself. Also, please don't post very long lines, like the ______ lines you used - it messes up the formatting of the post in the forum.

Is your question only about how to display a number as a binary string? See the method Integer.toBinaryString() for that (look it up in the API documentation).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
duhit Choudhary
Ranch Hand

Joined: Apr 01, 2012
Posts: 64
i already did that but i dont know why i am not getting the desirable output through that also
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
Why are you using Strings rather than plain ints. You can use 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, etc. Check up about the bitwise & and | operators. You can use them to set bits in the number, and also to check whether a certain bit is 1 or 0. Use == 0 or != 0 as tests for bits set and not set.
duhit Choudhary
Ranch Hand

Joined: Apr 01, 2012
Posts: 64
where to use this exactly???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
In a little class which you will write to try it out. When you have got it working in a little class, you will know how to use it in your lights application.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8159
    
  23

duhit Choudhary wrote:I have made a program for a relay IA-3088, in which i have to ON and OFF the different sets relay lights via my program and i think i succeeded in doing that. my program is also able to show the status if the rely lights will manually On and OFF but the problem is the status it is showing for different combinations of ON and OFF lights is hex form and i want to show the result i the binay form like if light 1, 2 and 8 are on & rest are of then status would be 10000011. here is my code with different four classes.
...
please help me in getting the desired output and to find any other flaw in the code.... it will be a great help. thanks a lot in advance

Right, well you seem to be getting good advice about the specifics, so I'll concentrate on the design.

Your classes are very tightly coupled; and specifically, they're tightly coupled to
1. Your input protocol.
2. Your I/O implementation (ie, all that CommPort stuff).
3. The number of lights.
4. The implementation of turning them on and off (ie, all those bitwise manipulations).

To me, a Light is a Light is a Light - ie, a glorified Boolean - it has two states: ON or OFF and probably needs some methods to turn it on or off and maybe also to check its current state. HOW that's done is none of its concern.

You also have messages coming in via a COM port. Fine. Write a class that deals with doing that - and DON'T worry about what those messages contain. Just write a comms class (or classes) that can reliably get a stream of characters from a CommPort and put them into a String.

The third part is translating those messages and THAT's the job of your LightManager. It should be able to take a String from your comms class and translate it into a set of calls to turn your lights on or off, and it should work whether your have 2 Lights or 2,000.

Don't get hung up on implementation details before you've got the design right.

HIH

Winston

BTW - If you're interested in a possible implementation for a bunch of lights, my suggestion would be to look at BitSet.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
I’d forgotten about BitSet. It allows you to check whether bits are 1 or 0, only it calls them true and false. Note the similarity of its methods to the &| I mentioned earlier.
duhit Choudhary
Ranch Hand

Joined: Apr 01, 2012
Posts: 64


i have made a change in Reader class but now i am getting an Exception


Exception in thread "Thread-1" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at retry_relay.Reader.hexToBin(Reader.java:51)
at retry_relay.Reader.run(Reader.java:30)
at java.lang.Thread.run(Unknown Source)
what is the problem now???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
Get rid of that dreadful block of if-elses.
You can get binary numbers from methods of the Integer class or similar.
duhit Choudhary
Ranch Hand

Joined: Apr 01, 2012
Posts: 64
problem got solved and the program is working now with desirable output. thank to all of you guys. But now if i want to make this code to be used in GUI then how and from where can i begin???
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Data Conversion