This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Plz help!     string class question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Plz help!     string class question" Watch "Plz help!     string class question" New topic

Plz help! string class question

zoster gibrilian

Joined: Sep 17, 2003
Posts: 7
I have to define by "hard coding!?" a 32-bit binary string that represents an IP address.
1)Can u help me with a command to do this?
2)What is the bit-length of the testNum string below?
string testNum = "10000010111101010001101100000010";
3)What is hard coding?
Ouaknin lionel

Joined: Sep 12, 2003
Posts: 14
I can't say I fully understood your question....
- Hard coding means putting a litteral value in the code instead of getting this value through user input or file.
- an IP address is made of 4 bytes. a byte = 8 bit. A byte in java has the type

a byte can take a value from -127 to 127.
An integer is also encoded in 4 for bytes. You can put an IP address into an integer or an array of 4 bytes.
for the array it is simple:

for an integer use hex i'm a bit lost...

"Nobody will ever need more than 256 kb of ram" -Bill Gates
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Terms like "binary string" are always a bit ambiguous. You did show a correct string representation of 32 1s and 0s which sounds like what was asked for. Are you comfortable converting integer to binary and back?
IP addresses are usually given as 4 numbers, each between 0 and 255, like You can make each number an 8 bit binary and get your 32 bit string.
IP doesn't always interpret the address as 8-bit chunks. I forgot the particulars as soon as my networking class was over, but it can use 6-bit chunks or 10 bits or whatever the addressing scheme of the day requires. Going that deep into addressing is about the only reason I can think of to worry about binary representations. Which means, I'm curious about what you're up to!

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
I agree. Here's the link:
subject: Plz help! string class question
It's not a secret anymore!