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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2018-09-07 Java【获取客户端真实IP地址】
2018-09-07 并发工具
2018-09-07 并发执行任务