zoukankan      html  css  js  c++  java
  • java Byte 和byte 差别及byte[ ]和string转换

      先看Byte,是一个类:

    public final class Byte

    The Byte class wraps a value of primitive type byte in an object. An object of type Byte contains a single field whose type is byte.

    In addition, this class provides several methods for converting a byte to a String and a String to a byte, as well as other constants and methods useful when dealing with a byte.

    byte是基本数据类型 
    Byte是byte的包装类 

    我们可以看出Byte是一个类,byte只是一个原始数据类型。Byte是引用类型,byte是值类型(原型), Byte是一个类,有很多方法,方便我们转换为其他类型.

    转换Byte【】到string
    public class Main {
        
        /*
         * This method converts an byte array to a String object.
         */
        
        public void convertByteArrayToString() {
            
            byte[] byteArray = new byte[] {87, 79, 87, 46, 46, 46};
            
            String value = new String(byteArray);
            
            System.out.println(value);
        }
        
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            new Main().convertByteArrayToString();
        }
    }

    string 转byte【】 

    String str;

    byte[] a=str.getBytes[];

    总结:

    String s = "fs123fdsa";//String变量 
    
    byte b[] = s.getBytes();//String转换为byte[] 
    
    String t = new String(b);//bytep[]转换为String

     

  • 相关阅读:
    ssh环境的搭建,基于注解和配置文件使用
    myeclipse db browser 新建数据源
    区别jquery对象和dom对象及转换方法
    C++中map容器的说明和使用技巧
    csh shell 语法 例子
    如何选择显示器
    Vim简明教程【CoolShell】
    perl
    perlretut
    ssh 配置详解
  • 原文地址:https://www.cnblogs.com/youxin/p/2601674.html
Copyright © 2011-2022 走看看