Java两种方法将int型转化为十六进制

系统方法和自定义方法

package com.example.administrator.myapplication;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * @see Testing documentation
 */
public class ExampleUnitTest {
    @Test
    public void addition_isCorrect() {
        assertEquals(4, 2 + 2);
        for (int i = 10; i < 20; i++) {
            int a = i;
            System.out.println(i+"系统方法:"+Integer.toHexString(a));
            System.out.println(i+"自定义方法:"+byteToHex(a));
        }
    }

    /**
     * 将一个整形化为十六进制,并以字符串的形式返回
     */
    private final static String[] hexArray = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    public String byteToHex(int n) {
        if (n < 0) {
            n = n + 256;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return hexArray[d1] + hexArray[d2];
    }

}

输出结果

H:\Android\androidstudio\jre\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 -Didea.launcher.port=50319 -Didea.launcher.bin.path=H:\Android\androidstudio\bin -Dfile.encoding=UTF-8 -classpath H:\Android\androidstudio\lib\idea_rt.jar;H:\Android\androidstudio\plugins\junit\lib\junit-rt.jar;H:\Android\androidstudio\plugins\junit\lib\junit5-rt.jar;C:\Users\Administrator\AppData\Local\Android\Sdk\platforms\android-28\data\res;C:\Users\Administrator\AndroidStudioProjects\MyApplication\app\build\intermediates\javac\debugUnitTest\compileDebugUnitTestJavaWithJavac\classes;C:\Users\Administrator\AndroidStudioProjects\MyApplication\app\build\intermediates\javac\debug\compileDebugJavaWithJavac\classes;C:\Users\Administrator\AndroidStudioProjects\MyApplication\app\build\generated\res\rs\debug;C:\Users\Administrator\AndroidStudioProjects\MyApplication\app\build\generated\res\resValues\debug;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\customview-28.0.0.aar\ce511edc59434dc855f03fec2ae1d370\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\localbroadcastmanager-28.0.0.aar\4894eeee94ab2e8dd45e9275f7aa2516\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\support-vector-drawable-28.0.0.aar\6b40d040409d8ea9bb26d9a07ee1ef4d\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\interpolator-28.0.0.aar\46f2a07d99cdfe7a4a077728aca45658\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\support-core-utils-28.0.0.aar\cc00a26c370fed68772d6b2af589e4b6\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\support-core-ui-28.0.0.aar\0c95a78ce02fe774ce1ba83f6f017a22\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\slidingpanelayout-28.0.0.aar\7a2f1c751850a9518708e4dccb9679cd\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\viewmodel-1.1.1.aar\3fd3ea6893df01b96b091f37ad5e3650\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\drawerlayout-28.0.0.aar\9ef6516851b6b64fd9951506d1b10738\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\coordinatorlayout-28.0.0.aar\d77ff869019a2c6baff26ccd670fe9ee\res;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\coordinatorlayout-28.0.0.aar\d77ff869019a2c6baff26ccd670fe9ee\jars\classes.jar;C:\Users\Administrator\.gradle\caches\modules-2\files-2.1\com.android.support\collections\28.0.0\c1bcdade4d3cc2836130424a3f3e4182c666a745\collections-28.0.0.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\documentfile-28.0.0.aar\16f9f52f843967fe33850271f44bf505\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\constraint-layout-1.1.3.aar\b32f4b5246acd98a37bcbbe33a1eb2d3\res;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\constraint-layout-1.1.3.aar\b32f4b5246acd98a37bcbbe33a1eb2d3\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\swiperefreshlayout-28.0.0.aar\876a9dea8ea962e068c562b1ea7f386f\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\cursoradapter-28.0.0.aar\02730ff1cf62820e89d05320862d5914\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\asynclayoutinflater-28.0.0.aar\df182cacc53e2eea194edb6e1f30a0a8\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\livedata-1.1.1.aar\5bb19ac6187771f18a5a7f21b74ec937\jars\classes.jar;C:\Users\Administrator\.gradle\caches\modules-2\files-2.1\android.arch.core\common\1.1.1\e55b70d1f5620db124b3e85a7f4bdc7bd48d9f95\common-1.1.1.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\versionedparcelable-28.0.0.aar\db8e364653bdb6fb622ba8d754b43e1c\jars\classes.jar;C:\Users\Administrator\.gradle\caches\modules-2\files-2.1\junit\junit\4.12\2973d150c0dc1fefe998f834810d68f278ea58ec\junit-4.12.jar;C:\Users\Administrator\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-core\1.3\42a25dc3219429f0e5d060061f71acb49bf010a0\hamcrest-core-1.3.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\runtime-1.1.1.aar\b7d9e96f94e41f15287c083d2fefaa11\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\print-28.0.0.aar\e5720335cdb8d5ed0e5a8894baa0eed2\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\loader-28.0.0.aar\827c62bae14a9c5c1fb8ee6bdb8c2fce\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\viewpager-28.0.0.aar\fe9bee3fcc10e0a4b325e0abc2b3a945\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\support-fragment-28.0.0.aar\e36a8547ac3222dc1621a382b2bc5da2\jars\classes.jar;C:\Users\Administrator\.gradle\caches\modules-2\files-2.1\com.android.support\support-annotations\28.0.0\ed73f5337a002d1fd24339d5fb08c2c9d9ca60d8\support-annotations-28.0.0.jar;C:\Users\Administrator\.gradle\caches\modules-2\files-2.1\android.arch.lifecycle\common\1.1.1\207a6efae6a3555e326de41f76bdadd9a239cbce\common-1.1.1.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\livedata-core-1.1.1.aar\105be965c1daf66446a38257af45698e\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\support-compat-28.0.0.aar\4648666595e9b28464a64f00b0eba1cc\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\support-compat-28.0.0.aar\4648666595e9b28464a64f00b0eba1cc\res;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\animated-vector-drawable-28.0.0.aar\3a868e4af8d9371fce45031cd026540e\jars\classes.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\appcompat-v7-28.0.0.aar\5ad333fe5576580c596c66257090671a\res;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\appcompat-v7-28.0.0.aar\5ad333fe5576580c596c66257090671a\jars\classes.jar;C:\Users\Administrator\.gradle\caches\modules-2\files-2.1\com.android.support.constraint\constraint-layout-solver\1.1.3\bde0667d7414c16ed62d3cfe993cff7f9d732373\constraint-layout-solver-1.1.3.jar;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\runtime-1.1.1.aar\63264d96bee727741134032b7e7ede52\jars\classes.jar;C:\Users\Administrator\AndroidStudioProjects\MyApplication\app\build\intermediates\sourceFolderJavaResources\test\debug;C:\Users\Administrator\AndroidStudioProjects\MyApplication\app\build\intermediates\sourceFolderJavaResources\debug;C:\Users\Administrator\.gradle\caches\transforms-1\files-1.1\android.jar\96b5b9e22aa47ddb722f4bbfd901d250\android.jar com.intellij.rt.execution.application.AppMainV2 com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 com.example.administrator.myapplication.ExampleUnitTest
10系统方法:a
10自定义方法:0a
11系统方法:b
11自定义方法:0b
12系统方法:c
12自定义方法:0c
13系统方法:d
13自定义方法:0d
14系统方法:e
14自定义方法:0e
15系统方法:f
15自定义方法:0f
16系统方法:10
16自定义方法:10
17系统方法:11
17自定义方法:11
18系统方法:12
18自定义方法:12
19系统方法:13
19自定义方法:13

Process finished with exit code 0

你可能感兴趣的:(Java基础)