Java String转byte[]

2023-12-26 18:48:07

String转byte[] 10进制

getBytes()

在Java中,可以使用 getBytes() 方法将字符串转换为字节数组。这方法有两种形式:

  1. getBytes(): 返回一个使用平台默认字符集编码的字节数组。
  2. getBytes(Charset charset): 返回一个使用指定字符集编码的字节数组。

以下是使用这两种方法的示例:

public class StringToByteExample {
    public static void main(String[] args) {
        // 示例字符串
        String myString = "Hello, World!";

        // 使用默认字符集编码
        byte[] bytesDefaultCharset = myString.getBytes();
        System.out.println("Bytes using default charset: " + new String(bytesDefaultCharset));

        // 使用指定字符集编码(例如UTF-8)
        try {
            // UTF-8字符集
            String charsetName = "UTF-8";
            byte[] bytesCustomCharset = myString.getBytes(charsetName);
            System.out.println("Bytes using " + charsetName + ": " + new String(bytesCustomCharset, charsetName));
        } catch (java.io.UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}

在使用指定字符集编码时,需要处理 UnsupportedEncodingException 异常。在实际使用中,可以根据具体需要选择合适的字符集,如UTF-8、ISO-8859-1等。

Android的默认编码

Android 里面默认使用UTF-8:

    /**
     * Returns the default charset of this Java virtual machine.
     *
     * <p>Android note: The Android platform default is always UTF-8.
     *
     * @return  A charset object for the default charset
     *
     * @since 1.5
     */
    public static Charset defaultCharset() {
        // Android-changed: Use UTF_8 unconditionally.
        synchronized (Charset.class) {
            if (defaultCharset == null) {
                defaultCharset = java.nio.charset.StandardCharsets.UTF_8;
            }

            return defaultCharset;
        }
    }

JDK默认编码

JDK默认编码是根据具体的JVM来的:

    /**
     * Returns the default charset of this Java virtual machine.
     *
     * <p> The default charset is determined during virtual-machine startup and
     * typically depends upon the locale and charset of the underlying
     * operating system.
     *
     * @return  A charset object for the default charset
     *
     * @since 1.5
     */
    public static Charset defaultCharset() {
        if (defaultCharset == null) {
            synchronized (Charset.class) {
                String csn = GetPropertyAction
                        .privilegedGetProperty("file.encoding");
                Charset cs = lookup(csn);
                if (cs != null)
                    defaultCharset = cs;
                else
                    defaultCharset = sun.nio.cs.UTF_8.INSTANCE;
            }
        }
        return defaultCharset;
    }

ASCII编码的协议可以用UTF-8的编码方式吗?

ASCII编码是一种使用7位(8位中的最高位不用)表示字符的编码方式,因此一个ASCII字符可以用一个字节(8位)表示。在Java中,字符串转换为ASCII编码的字节数组可以使用以下方式:

public class StringToAsciiExample {
    public static void main(String[] args) {
        // 示例字符串
        String myString = "Hello, ASCII!";

        // 将字符串转换为ASCII编码的字节数组
        byte[] asciiBytes = myString.getBytes();
        
        // 打印ASCII编码的字节数组
        for (byte b : asciiBytes) {
            System.out.print(b + " ");
        }
    }
}

上述代码将字符串 “Hello, ASCII!” 转换为ASCII编码的字节数组,并打印每个字节的值。请注意,由于Java的 byte 类型是有符号的,所以输出的值在0-127范围内表示正常的ASCII字符,而在128-255范围内表示扩展ASCII字符。如果你需要使用无符号字节值,可以通过将其转换为整数进行处理。

String转byte[] 16进制

如果你想要获取ASCII值的十六进制表示,你可以使用 Java 的 Integer.toHexString 方法将字节数组中的每个字节转换为十六进制字符串。下面是一个示例:

public class AsciiHexExample {
    public static void main(String[] args) {
        // 字符串 "1"
        String myString = "1";

        // 获取ASCII值的字节数组
        byte[] asciiBytes = myString.getBytes();

        // 打印ASCII值的十六进制表示
        for (byte b : asciiBytes) {
            // 将字节转换为无符号整数,然后转换为十六进制字符串
            String hexValue = Integer.toHexString(b & 0xFF).toUpperCase();

            // 输出十六进制表示
            System.out.println("ASCII value in Hex: " + hexValue);
        }
    }
}

在这个示例中,Integer.toHexString(b & 0xFF) 将字节转换为无符号整数,并使用 toUpperCase() 将十六进制字符串转换为大写形式。

输出应该是:

ASCII value in Hex: 31

这里的 “31” 是字符 ‘1’ 对应的ASCII值 49 的十六进制表示。

文章来源:https://blog.csdn.net/Shujie_L/article/details/135227332
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。