软帝学院:java十进制八进制十六进制数互相转换

Posted 王囧草

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了软帝学院:java十进制八进制十六进制数互相转换相关的知识,希望对你有一定的参考价值。

java-十进制、八进制、十六进制数互相转换

下面一个十进制、八进制、十六进制数互相转换的小程序

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

public class JZTurn {

 

    public static void main(String[] args) {

        int num2 = 1314521;

        System.out.println("十进制数:"+num2);

        System.out.println("十进制转换二进制:"+turn2(num2));

        System.out.println("十进制转换八进制:"+turn8(num2));

        System.out.println("十进制转换十六进制:"+turn16(num2));

         

        //2-> 8

        System.out.println("二进制转换八进制:"+twoTurnEight(turn2(num2)));

        System.out.println("八进制转换二进制:"+eightTurnTwo(turn8(num2)));

        System.out.println("八进制转换十六进制:"+eightTurnSixteen(turn8(num2)));

        //2->16

        System.out.println("二进制转换十六进制:"+twoTurnSixteen(turn2(num2)));

    }

     

    /**

     * 10进制转二进制

     * @param num

     * @return

     */

    public static String turn2(int num){

        String shortTimeNum = "";

        int remainder = 0;

        /**

         * 模拟短除法

         */

        while(num >= 1){

            remainder = num % 2;

            num = num / 2;

            shortTimeNum = remainder + shortTimeNum;

        }

         

        return shortTimeNum;

    }

     

    /**

     * 10进制转8进制

     * @param num

     * @return

     */

    public static String turn8(int num){

        String shortTimeNum = "";

        int remainder = 0;

        /**

         * 模拟短除法

         */

        while(num >= 1){

            remainder = num % 8;

            num = num / 8;

            shortTimeNum = remainder + shortTimeNum;

        }

         

        return shortTimeNum;

    }

     

    /**

     * 10进制转16进制

     * @param num

     * @return

     */

    public static String turn16(int num){

        String shortTimeNum = "";

        int remainder = 0;

        String tempStr = "";

        /**

         * 模拟短除法

         */

        while(num >= 1){

            remainder = num % 16;

            tempStr = getOtherNum(remainder);

             

            num = num / 16;

            shortTimeNum = tempStr + shortTimeNum;

        }

         

        return shortTimeNum;

    }

     

    /**

     * 2进制转换8进制

     * 概念说明:这里转换的是整数,从右向左三位一组分别乘以2的零次方,2的一次方,2的2次方

     * 然后把每组中的数相加,再把各组从左向右拼接到一起

     * @param strNum2

     */

    public static String twoTurnEight(String strNum2){

        String resultNum = "";

        int remainder = strNum2.length() % 3;

         

        //补位 三位一组,最后一组位数不够用0补充

        switch(remainder){

        case 1:

            strNum2 = "00"+strNum2;

            break;

        case 2:

            strNum2 = "0"+strNum2;

            break;

        }

        int index = strNum2.length()-1;

        int tempNum1 = 0;

        int tempNum2 = 0;

        int tempNum3 = 0;

        while(index>=1){

 

            index -- ;

             

            if(index % 3 == 0){

                //每个数为一组

                tempNum1 = Integer.parseInt(strNum2.substring(index, index+1));

                tempNum2 = Integer.parseInt(strNum2.substring(index+1, index+2));

                tempNum3 = Integer.parseInt(strNum2.substring(index+2, index+3));

                 

                resultNum = tempNum3*1+tempNum2*2+tempNum1*4+resultNum;

                 

            }

             

        }

         

        return resultNum;

    }

     

    /**

     * 八进制转换二进制

     * @param eightNum

     * @return

     */

    public static String eightTurnTwo(String eightNum){

        String twoNum = "";

        int index = eightNum.length()-1;

         

        String tempNum = "";

        while(index>=0){

             

            tempNum = turn2(Integer.parseInt(eightNum.substring(index, index+1))) ;

             

            //补位,在转换8进制时是每三为二进制数为一组,转换回二进制时位数也需要是三位,不够用零补

            switch (tempNum.length()) {

             

            case 1:

                tempNum = "00"+tempNum;

                break;

 

            case 2:

                tempNum = "0"+tempNum;

                break;

            }

             

            twoNum = tempNum + twoNum;

            index--;

        }

         

        return twoNum;

    }

     

    /**

     * 2进制转换16进制

     * 概念说明:这里转换的是整数,从右向左四位一组分别乘以2的零次方,2的一次方,2的2次方,2的3次方,

     * 然后相加把每组最终的得数一次从左向右拼到一起,若其中一组的和大于9,按照对应关系转换后再把每组的结果拼接到一起

     * @param strNum2

     */

    public static String twoTurnSixteen(String strNum2){

        String resultNum = "";

        int remainder = strNum2.length() % 4;

         

        //补位,四位一组,最后一组位数不够用0补充

        switch(remainder){

        case 1:

            strNum2 = "000"+strNum2;

            break;

        case 2:

            strNum2 = "00"+strNum2;

            break;

        case 3:

            strNum2 = "0"+strNum2;

            break;

        }

        int index = strNum2.length()-1;

        int tempNum1 = 0;

        int tempNum2 = 0;

        int tempNum3 = 0;

        int tempNum4 = 0;

        int tempNum5 = 0;

        while(index>=1){

 

            index -- ;

             

            if(index % 4 == 0){

                //每个数为一组

                tempNum1 = Integer.parseInt(strNum2.substring(index, index+1));

                tempNum2 = Integer.parseInt(strNum2.substring(index+1, index+2));

                tempNum3 = Integer.parseInt(strNum2.substring(index+2, index+3));

                tempNum4 = Integer.parseInt(strNum2.substring(index+3, index+4));

                 

                tempNum5 = tempNum4*1 + tempNum3*2+tempNum2*4+tempNum1*8;

                 

                resultNum = getOtherNum(tempNum5) +resultNum;

                 

            }

             

        }

         

        return resultNum;

    }

     

    /**

     * 8进制转换16进制

     * @param strNum2

     */

    public static String eightTurnSixteen(String strNum2){

        return twoTurnSixteen(eightTurnTwo(strNum2));

    }

     

    /**

     * 16进制转换中的特殊处理,需要把大于9的数字转换成字母

     * @param tempNum

     * @return

     */

    public static String getOtherNum(int tempNum){

        String tempStr = "";

        if(tempNum > 9){

            switch(tempNum){

            case 10:

                tempStr = "A";

                break ;

            case 11:

                tempStr = "B";

                break;

            case 12:

                tempStr = "C";

                break;

            case 13:

                tempStr = "D";

                break;

            case 14:

                tempStr = "E";

                break;

            case 15:

                tempStr = "F";

                break;

            }

        }else{

            tempStr = String.valueOf(tempNum);

        }

        return tempStr;

    }

}

输出结果:

十进制数:1314521
十进制转换二进制:101000000111011011001
十进制转换八进制:5007331
十进制转换十六进制:140ED9
二进制转换八进制:5007331
八进制转换二进制:101111011011001
八进制转换十六进制:5ED9
二进制转换十六进制:140ED9

喜欢这样文章的可以关注我,我会持续更新,你们的关注是我更新的动力!需要更多java学习资料的也可以私信我!

祝关注我的人都:身体健康,财源广进,福如东海,寿比南山,早生贵子,从不掉发!

以上是关于软帝学院:java十进制八进制十六进制数互相转换的主要内容,如果未能解决你的问题,请参考以下文章

16进制与10进制怎么互相转换

进制之间互转

8421BCD码和十进制数互相转换 (100101111000)8421BCD (19.7)10

二进制数与十六进制数之间如何互相转换(看完秒懂)

10进制转换2进制的互相转换

新手求问lua能将十进制和十六进制数互相转化吗