[Java] I want to convert a byte array to a hexadecimal number

Thing you want to do

I want to change the character code "A" to UTF-16

Expected results

Decimal number: 48 (10) 46 (10) Hexagon: 30 (16) 42 (16)

Realization method

Use String.Format or System.out.printf

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String stringValue = "Ah";
        byte [] mojiCode = stringValue.getBytes(StandardCharsets.UTF_16BE);
        System.out.println(mojiCode);        //[B@372f7a8d
        System.out.println(  Arrays.toString(mojiCode)); //[48, 66]
        for (Byte code : mojiCode) {
            System.out.printf("%02x", code); //3042
        }
        System.out.println("");
        StringByteArray stringByteArray = new StringByteArray(mojiCode);
        System.out.println(stringByteArray.toHexString()); //3042
    }
    static class StringByteArray {
        byte[] byteArray;
        public StringByteArray(byte[] byteArray) {
            this.byteArray = byteArray;
        }
        public String toHexString() {
            StringBuffer stringBuffer = new StringBuffer();
            for (byte code : byteArray) {
                String hexString = String.format("%02x", code);
                stringBuffer.append(hexString);
            }
            return stringBuffer.toString();
        }
    }
}

Recommended Posts

[Java] I want to convert a byte array to a hexadecimal number
Convert a Java byte array to a string in hexadecimal notation
How to convert a file to a byte array in Java
[Ruby] I want to put an array in a variable. I want to convert to an array
I want to ForEach an array with a Lambda expression in Java
I want to convert characters ...
[Java] Convert ArrayList to array
I want to make a list with kotlin and java!
I want to call a method and count the number
I want to convert InputStream to String
[Java] How to convert a character string from String type to byte type
I want to make a function with kotlin and java!
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to convert a string to a LocalDate type in Java
How to make a Java array
[Java] Convert array to ArrayList * Caution
I want to convert an array to Active Record Relation with Rails
I want to develop a web application!
I want to write a nice build.gradle
I want to write a unit test!
I want to stop Java updates altogether
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I want to create a dark web SNS with Jakarta EE 8 with Java 11
Run R from Java I want to run rJava
I want to send an email in Java.
java I tried to break a simple block
I did Java to make (a == 1 && a == 2 && a == 3) always true
Convert a string to a character-by-character array with swift
[Ruby] I want to do a method jump!
I want to use java8 forEach with index
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I want to simply write a repeating string
How to convert a solidity contract to a Java contract class
I want to design a structured exception handling
rsync4j --I want to touch rsync in Java.
I want to write quickly from java to sqlite
I tried to break a block with java (1)
[Java] How to convert one element of a String type array to an Int type
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
I want to play a GIF image on the Andorid app (Java, Kotlin)
How to ZIP a JAVA CSV file and manage it in a Byte array
Rails6 I want to make an array of values with a check box
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
I want to call a method of another class
I want to do something like "cls" in Java
[Java] I want to calculate the difference from the date
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I learned stream (I want to convert List to Map <Integer, List>)
I want to use a little icon in Rails
I tried to create a Clova skill in Java
I want to monitor a specific file with WatchService
I tried to make a login function in Java
I want to define a function in Rails Console
I want to transition screens with kotlin and java!
How to convert A to a and a to A using AND and OR in Java
I want to add a reference type column later
I want to click a GoogleMap pin in RSpec
Gzip-compress byte array in Java and output to file
I want to get along with Map [Java beginner]
I want to connect to Heroku MySQL from a client
I want to create a generic annotation for a type