my dog learned polymorphism*
The moose likes Beginning Java and the fly likes stuck in bitwise operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "stuck in bitwise operators" Watch "stuck in bitwise operators" New topic
Author

stuck in bitwise operators

Aj Mathia
Ranch Hand

Joined: Apr 11, 2003
Posts: 478
Hi All,
I am trying to create a file which i will later send to a cobol based machine . so i have to compress numbers in comp3 format.
to achieve this i need to represent 2 numbers in one byte.
eg to send the number 12 what i do is
0x0010 | 0x0002 which returns 0x0012
etc and write the result in a file
in all cases from 00 to 79 it works fine but 80 82-89 91-99
for all these mentioned numbers i am getting the result of the OR operation as 0x003f
ie 0x0080 | 0x0000 returns a 0x003f
can anyone tell me what simple stuff i am missing
and also if any one has any java utility or approach for decimal to COMP-3 conversions
Thanks


You think you know me .... You will never know me ... You know only what I let you know ... You are just a puppet ... --CMG
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Try a forum search on COMP3 or packed decimal. We had a thread on this earlier in the year. I may still have some code on another PC that I can bring back.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Ajay,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
i am getting the result of the OR operation as 0x003f
ie 0x0080 | 0x0000 returns a 0x003f
can anyone tell me what simple stuff i am missing


What code are you using to create such a result?
Aj Mathia
Ranch Hand

Joined: Apr 11, 2003
Posts: 478
Hi Dirk,
Firstly i chave changed the fisplay name to include my surname as well
was that the problem? coz i had a quick read through the naming policy and could not figure out any specific problem as per my display name provided
if Ajay is offencive then im sorry but thats my name
ok coming to the problem
i have made a method as follows
public void convertToComp3(int num1, int num2){
int[] a = {0x0000,0x0010,0x0020,0x0030,
0x0040,0x0050,0x0060,0x0070,0x0080,0x0090};
int[] b = {0x0000,0x0001,0x0002,0x0003,
0x0004,0x0005,0x0006,0x0007,0x0008,0x0009};
num1 = a[num1];
num2 = b[num2];
// open a file in append mode
int result = (num1 | num2);
// write(result);
}

calling method{ // pls ignore syntax
for i = 0 to 9;
for j = 0 to 9;
convertToComp3(i,j);
}

now when i open the created file in a hex editor or as a hex file
i have entries
0000
0001 etc till 0079 then insted of 0080 i have 003f this is from 80 to 99 except for 2 entries that i mentioned abouve the 80 and the other one

i changed the int a[]={0x000F, 0x001F, 0x002F .....} and b[]={0x00f0, 0x00f1,....}
and the step result = (num1 & num2) but got the same result.
hope i am clear in what i explained
Thanks
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
i chave changed the fisplay name to include my surname as well
was that the problem? coz i had a quick read through the naming policy and could not figure out any specific problem as per my display name


Yes. We require display names of the pattern first name + SPACE + a last name. I agree that the naming policy document doesn't currently make that immediately clear. I'll see about changing it.

Thanks.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I just wrote a program following your posted logic, and it worked fine.

Perhaps you're doing something while writing the results to the file that isn't working as expected. What does that code look like?

(I don't think it's relevant, but note that I don't have a hex editor, so I'm merely looking at regular text results.)
[ July 23, 2004: Message edited by: Dirk Schreckmann ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Here's what I had from last time this came up ... I hope the test will show how the pd class can be used ...

and
Aj Mathia
Ranch Hand

Joined: Apr 11, 2003
Posts: 478
Hi Dirk, Stan,
thanks Stan for your swift reply.
I had a quick scan through the code you gave me and it works fine till the point where my initial problem comes in.
I am quite certain that it is becoz int, byte etc is acting differently the moment the sigh bit is set ie for 0x0080 80 is repesented as 1000 0000
in int and byte does not accommodate this number either.
Stan in the code you gave me if i try
PackedDecimal pd = new PackedDecimal(7779821,4 ); or
PackedDecimal pd = new PackedDecimal(9874321,4 );
the 98 is not handled right.
similarly
byte[] lBytes = new byte[] { 0x80, 0x54, 0x32, 0x1c };
PackedDecimal pd = new PackedDecimal(lBytes);
throws a compilation error as 0x80 is out of range for byte.
any tips as to how i can handle this

Dirk my file writing routine is very basic as this is the first time i am writing to a file
try {
BufferedWriter out = new BufferedWriter(new FileWriter("am.txt", true));
out.write(myByte);
out.close();
}
catch (IOException e) {
e.printStackTrace();
}

i also tried out.write(128) ie 128 hex value is 80
i tried int a=0x0100;
a= a>>1;
out.write(a);
but all the cases result is 0x3f

to see the hex values i use textpad and when i open the am.text(output file)
i select the option view binary.

i really appreciate your responses
Thanks
Aj Mathia
Ranch Hand

Joined: Apr 11, 2003
Posts: 478
Hi guys
I figured out where i was going wrong.
the problem was i was using the BufferedWriter to write to the file.
and there is no character associated with hex 80 or 82,83....99
so it by default puts in a ? ie 0x3f
when i changed the file approach to DataOutputStream it puts the hex values just fine
Thanks a lot for your help till now
Ajay
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: stuck in bitwise operators
 
Similar Threads
NX: how to fill long[] results in findByCriteria()
anonumous arrays?
FTP commons.net FTP never ends
Webservice client program to return an xml file
NX: Contactor: Query on Search method