WindChat/windchat-business/src/main/java/com/windchat/im/business/utils/HexUtils.java

43 lines
1.2 KiB
Java
Executable File

/**
* Copyright 2018-2028 Akaxin Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.windchat.im.business.utils;
/**
* byte数组与字符串之间转换
*/
public class HexUtils {
public static String bytesToHexString(byte[] b) {
StringBuffer returnValue = new StringBuffer();
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
returnValue.append(hex.toUpperCase() + " ");
}
return returnValue.toString();
}
public static void main(String[] args) {
String test = "你好,我是中国人";
System.out.println(bytesToHexString(test.getBytes()));
}
}