Converts a hex number into a decimal number. - Java java.lang

Java examples for java.lang:Hex

Description

Converts a hex number into a decimal number.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int[] data = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(hexToDec(data));
    }/*  w  w w . java 2  s  .  c o  m*/

    /**
     * Converts a hex number into a decimal number.
     *
     * @param data the digits of a hex number
     * @return the decimal number
     */
    public static int hexToDec(int[] data) {
        int dec = 0;
        for (int i = 0; i < data.length; i++) {
            dec += (int) Math.pow(256, data.length - 1 - i) * data[i];
        }
        return dec;
    }
}

Related Tutorials