This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Murach's Java Programming and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Plz help! string class question  RSS feed

 
zoster gibrilian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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?
Thanks
 
Ouaknin lionel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 values.here i'm a bit lost...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 11.96.8.175. 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!
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
Thoughts on deprecation in Java
https://coderanch.com/t/683016/java/Deprecation-Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!