take a character and return its hex equivalent - Java java.lang

Java examples for java.lang:Hex

Description

take a character and return its hex equivalent

Demo Code

/*******************************************************************************
 * Copyright (c) 2004, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * /*from  www  .j  a  va2 s  .com*/
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package com.java2s;
import java.io.File;

public class Main {
    public static void main(String[] argv) throws Exception {
        char ch = 'a';
        System.out.println(mangleChar(ch));
    }

    /**
     * take a character and return its hex equivalent
     */
    private final static String mangleChar(char ch) {
        if (ch == File.separatorChar) {
            ch = '/';
        }
        if (Character.isLetterOrDigit(ch) == true) {
            return "" + ch; //$NON-NLS-1$
        }
        return "_" + Integer.toHexString(ch).toUpperCase() + "_"; //$NON-NLS-1$ //$NON-NLS-2$
    }
}

Related Tutorials