• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

byte array initialization

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, can I initialize a byte array like this?
byte array[]=new array[]{255,255,255,0}; the compliler says cast from int to byte is needed. I don't understand why it needs cast.
btw, anyone would tell me how to install and show an IP address in the program? (not network programming, just input, store and output the IP address)
thanks
ideal
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For ur first Question,
the values u have specified for ur byte array are beyond the max limit of what a byte can hold
Byte has 8 bits of storage and can store from
-128 to 127
This wud work ....
byte[] array=new byte[]{127,-128,0};
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ideal:
byte array[]=new array[]{255,255,255,0}; the compliler says cast from int to byte is needed. I don't understand why it needs cast.


255 won't fit into a byte. Try
byte array[]=new array[]{127,127,127,0};

Please read http://www.javaranch.com/name.jsp and change your name to conform. We'd like you to continue to post here at JavaRanch. Thanks.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ideal
Welcome to the Java Ranch, we hope you’ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please re-register under an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!

As for your question:
The literal 255 is an int, in order to store it in a byte variable you have to cast it to a byte. But when you do that you'll lose some of the info because 255 is larger than a byte (-128 to 127).
As for your second question:
Input the ip how? From the command line as an argument to main? or as an input when prompted for it?
As far as storing and printing the ip - where do you want it stored (disc, text file, database, ...) and to what do you want it printed (printer, screen, dialogbox, ...). Do you have any code so far? post it so we can see where you're at. Sorry for so many questions but you need to give some more detail to get any meaningful help.
Also, this sounds like a school asignment so you might not get complete answers but little nudges in the right direction.
[ January 31, 2002: Message edited by: Dave Vick ]
 
Ideal Cana
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks the responses. I changed my name to "maple", is that fine?
Dave, my task is to accept a destination IP address from the command line, then simulate the procedure to look up the next hop to this destination in a routing table. I should difine this routing table with the given contents. for example:
routing table:
mask destination nexthop...
255.255.255.0 192.168.1.0 192.168.2.1
I don't have idea how to define this table.
maple
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maple,
Did you READ the naming policy?? It says that your display name should have at least 2 (as in two) separate names, with a space in between. Each name should be more than one letter. The name should not be obviously fictitious (such as "Maple"}. It says that we REALLY want you to use your real name.
Please try again.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "next hop"? Is there a pre-defined routing or are you talking about the nodes in the IP?
 
Ideal Cana
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the name policy. Believe it or not, my nick name is Ideal.
yes, the contents of the routing table is pre-difined. Now I need to show this table with Java in computer.
I'm thinking to use String and StringTokenizer. Any suggestion? btw, nexthop is just a column name of this table, its value is also IP address.
Ideal
reply
    Bookmark Topic Watch Topic
  • New Topic