This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

File Doubt  RSS feed

 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
public class fileobj3
{
public static void main(String args[]) throws IOException
{
byte[] buf;
String s = "12345678687687989808080768787878";
buf=s.getBytes();
ByteArrayInputStream b = new ByteArrayInputStream(buf);
DataInputStream d = new DataInputStream(b);
System.out.println(d.readInt());
System.out.println(d.readUnsignedByte());
System.out.println(d.readUnsignedShort());
}
}
--------------------------------------------------
When I run the above program I get the following output
--------------------------------------------------
825373492
53
13879
--------------------------------------------------
Can someone explain please...
Thanks
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The readInt() method interprets the next four bytes into an integer. In your case, the 1st 4 bytes of the string i.e., the string "1234" is re-interpreted at the binary level into an integer.
and so on with the other two.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's all in the bits. Let's look at a shortened version:

As we can see, each "0" in the String is turning into a byte of 48. Mysterious until we realize that 48 is hex 30 which is the charset representation of zero. So if we get out our handy hex to decimal converter and type in 30303030 and convert that to decimal we get 808464432 which is what the above program prints out.
So let's look at your example:
For the int "1234" converts to bytes in decimal = 49-50-51-52 which converts to 31-32-33-34 in hex. Converting the number 31323334 to decimal gives: 825373492 which is exactly the answer. I will leave the rest an an excercise to the reader.
[ October 10, 2002: Message edited by: Thomas Paul ]
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Thomas,Vin,
Thanks.. Am able to understand now...
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really? Then maybe you can explain it to me.
 
Hot dog! An advertiser loves us THIS much:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!