Solved

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

Posted on 1998-08-07
6
228 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now