The Artima Developer Community
Sponsored Link

Java Answers Forum
Conversion from decimal to dotted decimal base 256 number

2 replies on 1 page. Most recent reply: Apr 25, 2003 6:43 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Courtenay

Posts: 23
Nickname: courtz
Registered: Apr, 2003

Conversion from decimal to dotted decimal base 256 number Posted: Apr 25, 2003 7:52 AM
Reply to this message Reply
Advertisement
I need to write a program that takes as its input a decimal number and produces as its output a dotted-decimal base 256 number.

Can anyone help?


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Conversion from decimal to dotted decimal base 256 number Posted: Apr 25, 2003 4:59 PM
Reply to this message Reply
Can you explain the problem with an example please

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Conversion from decimal to dotted decimal base 256 number Posted: Apr 25, 2003 6:43 PM
Reply to this message Reply
/*
 * IpFormatter.java
 *
 * Simple demo of converting an int to a IP-format string.
 *
 * Copyright 2003, Matt Gerrans.
 *
 * Turning in this code as homework is a violation of ethical principals,
 * and punishable by severe penalties, even if you change the variable
 * names.   Feeding it to your dog is acceptible.
 */
class IpFormatter
{
   static String getIpFormat( int number )
   {
      StringBuffer buffy = new StringBuffer();
 
      for( int i = 0; i < 4; i++ )
      {
         int part = 0xff & (number >> ((3-i)*8));
         buffy.append( i > 0 ? "." : "" );
         buffy.append( Integer.toString(part));
      }
      return buffy.toString();
   }
 
   public static void main(String args[])
   {
      int N = 0xffffffff;    // 255.255.255.255.
 
      try
      {
         N = Integer.parseInt(args[0]);
      }
      catch( java.lang.ArrayIndexOutOfBoundsException aiobe )
      {
      }
      catch( java.lang.NumberFormatException nfe )
      {
         System.out.println( "Syntax: " + "\n"
                             "   java IpFormatter [N]" + "\n"
                             "Where N is an integer value." );
      }   
 
      System.out.println( N + " -> " + getIpFormat(N) );
   }
}

Flat View: This topic has 2 replies on 1 page
Topic: Making Events work from different classes Previous Topic   Next Topic Topic: Hell Mr  dhrubo

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use