• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

stuck in bitwise operators

 
Ranch Hand
Posts: 478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic