Int format and conversion : Format « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » Format 




Int format and conversion
 

/*
 * Created on Dec 27, 2005
 */

/*
 Copyright 2007 Robert C. Ilardi

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */


public class IntUtils {

  public static int IntLen(int i) {
    int len;
    int result;

    if (i >= 10) {
      len = 2;
      result = i / 10;
      while (result >= 10) {
        len++;
        result /= 10;
      }
    }
    else {
      len = 1;
    }

    return len;
  }

  public static byte[] IntToBytes(int i) {
    int digits, hiLimit, digit;
    byte[] bArr;

    digits = IntLen(i);
    bArr = new byte[digits];
    hiLimit = (intMath.pow(10, digits - 1);

    for (int j = 0; j < digits; j++) {
      digit = i / hiLimit;

      bArr[j(byte) (48 + digit);

      i -= (digit * hiLimit);
      hiLimit /= 10;
    }

    return bArr;
  }

  public static byte[] AddZeroPadding(byte[] bArr, int fixedLen) {
    byte[] flArr;
    int cpStartIndex;

    if (bArr.length >= fixedLen) {
      flArr = bArr;
    }
    else {
      flArr = new byte[fixedLen];
      cpStartIndex = fixedLen - bArr.length;

      //Pre-pend Zeros for Padding as needed...
      for (int i = 0; i < cpStartIndex; i++) {
        flArr[i(byte48;
      }

      System.arraycopy(bArr, 0, flArr, cpStartIndex, bArr.length);
    }

    return flArr;
  }

  public static int BytesToInt(byte[] bArr) {
    int cnt = 0, num = 0;

    for (int i = bArr.length - 1; i >= 0; i--) {
      num += (bArr[i48((intMath.pow(10, cnt++));
    }

    return num;
  }

  public static void main(String[] args) {
    byte[] bArr;
    int num;

    System.out.println(BytesToInt(AddZeroPadding("".getBytes()4)));
    System.out.println(BytesToInt(AddZeroPadding("1".getBytes()4)));
    System.out.println(BytesToInt(AddZeroPadding("23".getBytes()4)));
    System.out.println(BytesToInt(AddZeroPadding("555".getBytes()4)));
    System.out.println(BytesToInt(AddZeroPadding("1234".getBytes()4)));
    System.out.println(BytesToInt(AddZeroPadding("12345".getBytes()4)));

    for (int i = 0; i <= 1000000000; i++) {
      bArr = IntToBytes(i);
      num = BytesToInt(bArr);
      //System.out.println(num);
      if (num != i) {
        System.out.println(num + " != " + i);
        break;
      }
      else if (num % 1000000 == 0) {
        System.out.println(num);
      }
    }
  }

}

   
  














Related examples in the same category
1.String Align Demo: extends FormatString Align Demo: extends Format
2.Format SizeFormat Size
3.Get Percent Value
4.A format for String objects that is easier to persist
5.The # symbol shows a digit or nothing if no digit present
6.Make custom Input Text Formatter in Java
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.