Solved

Converting a 4 bytes long to a java 8 bytes long...

Posted on 1998-08-07
6
240 Views
Last Modified: 2010-05-18
I am reading a file that contains longs as 4 bytes, chars as 1 byte, ints as 2 bytes values..
I need to convert all the values to java native types.. (long as 8 byte, int as 4 byte and
char as 2 bytes)....

how can this be done...

can some code be included...
0
Comment
Question by:simlox
  • 4
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
mjenkins earned 20 total points
ID: 1231715
FileInputStream istream = new FileInputStream("t.tmp");
DataInputStream p2 = new DataInputStream(istream);
int i = p2.readInt(); // reads 4 byte value
int i1 = p2.readShort(); // reads 2 byte value
char c = (char)p2.readByte(); // reads 1 byte char
istream.close();

0
 
LVL 3

Expert Comment

by:mjenkins
ID: 1231716
Did this solve your problem?
0
 

Author Comment

by:simlox
ID: 1231717
Yes.. it worked perfectly..
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 3

Expert Comment

by:mjenkins
ID: 1231718
Then why did you give it a "D"? I'm curious. You must have expected more. I thought that the supplied code was sufficient an answer. Obviously I was mistaken.
0
 

Author Comment

by:simlox
ID: 1231719
I gave it a 'B'.... The other question on conversions
from a binary file using ObjectInputStream was given a
'D'..not this one.. I use the above information on the
same binary file..
0
 
LVL 3

Expert Comment

by:mjenkins
ID: 1231720
My mistake, I confused the questions. I'm still disappointed that you graded the other question a "D" .. It is kind of a matter of pride. Yours is the only D I've gotten thus far as an "expert" here. Oh, well...
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
runtime exception 2 42
throw exception 21 58
sql import cannot be resolved jsp 3 49
Java: anonymous class 4 29
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question