Java【Byte】

 官方文档:http://jszx-jxpt.cuit.edu.cn/javaapi/java/lang/Byte.html#parseByte(java.lang.String,%20int)

 1 public class test {
 2 
 3     public static void main(String[] args) {
 4 
 5         String name = "zhangsan";
 6         // 字符串转byte数组, 对应的每个字符会转成对应的ascii十进制值
 7         byte[] bytes = name.getBytes(StandardCharsets.UTF_8);
 8         System.out.println(Arrays.toString(bytes)); // [122, 104, 97, 110, 103, 115, 97, 110]
 9 
10         char[] chars = name.toCharArray();
11         for (int i = 0; i < chars.length; i++) {
12             System.out.print((byte) chars[i]);
13             if (i != chars.length - 1) {
14                 System.out.print(","); // 122,104,97,110,103,115,97,110
15             }
16         }
17 
18         // 将2进制00001000转成Byte类型
19         System.out.println(Byte.parseByte("00001000", 2)); // 二进制转10进制 -> 8
20         // 将16进制7F转成Byte类型
21         System.out.println(Byte.parseByte("7F", 16));
22 
23         // 将2进制的00001000转成Integer类型
24         System.out.println(Integer.parseInt("00001000", 2));
25         // 将16进制的FF 转成Integer类型
26         System.out.println(Integer.parseInt("FF", 16)); // 255
27     }
28 }

 

 

Byte源码

 1 public final class Byte extends Number implements Comparable<Byte> {
 2 
 3     public static final byte   MIN_VALUE = -128;
 4     public static final byte   MAX_VALUE = 127;
 5     public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
 6 
 7 
 8     // byte的缓存池, 就是ascii码中十进制的值从-128到127
 9     private static class ByteCache {
10         private ByteCache(){}
11         static final Byte cache[] = new Byte[-(-128) + 127 + 1];
12         static {
13             for(int i = 0; i < cache.length; i++)
14                 cache[i] = new Byte((byte)(i - 128));
15         }
16     }
17 
18     // 将一个字符串转成Byte
19     public static Byte valueOf(String s, int radix)
20     public static Byte valueOf(String s)
21     public static Byte valueOf(byte b) {
22         final int offset = 128;
23         return ByteCache.cache[(int)b + offset];
24     }
25 
26     // 将string参数解析为有符号的指定radix进制的byte
27     public static byte parseByte(String s, int radix)
28     // 将string参数解析为有符号的十进制的byte
29     public static byte parseByte(String s)
30 
31     public static Byte decode(String nm)
32 
33     // 构造函数
34     public Byte(byte value)
35     public Byte(String s)
36         
37     // Byte包装类型, 转成对应的基本类型
38     public byte byteValue()
39     public short shortValue()
40     public int intValue() 
41     public long longValue() 
42     public float floatValue() 
43     public double doubleValue() 
44         
45     // 转成字符串, 也就是这个byte类型的值对应的十进制值
46     public String toString()
47     public static String toString(byte b) {
48         return Integer.toString((int)b, 10);
49     }
50     
51     public int hashCode() 
52     public static int hashCode(byte value)
53     public boolean equals(Object obj)
54     public int compareTo(Byte anotherByte)
55     public static int compare(byte x, byte y)
56     public static int toUnsignedInt(byte x) 
57     public static long toUnsignedLong(byte x)
58     public static final int SIZE = 8;
59     public static final int BYTES = SIZE / Byte.SIZE;
60 }

 

posted @ 2022-09-07 20:42  为你编程  阅读(92)  评论(0)    收藏  举报