十六进制编码的字符串到字节数组 [英] Hex-encoded String to Byte Array

查看:20
本文介绍了十六进制编码的字符串到字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String str = "9B7D2C34A366BF890C730641E6CECF6F";

我想将 str 转换成字节数组,但是 str.getBytes() 返回 32 个字节而不是 16 个.

I want to convert str into byte array, but str.getBytes() returns 32 bytes instead of 16.

推荐答案

我认为提问者想要的是将十六进制值的字符串表示形式转换为表示该十六进制值的字节数组.

I think what the questioner is after is converting the string representation of a hexadecimal value to a byte array representing that hexadecimal value.

apache commons-codec 有一个类,十六进制.

The apache commons-codec has a class for that, Hex.

String s = "9B7D2C34A366BF890C730641E6CECF6F";    
byte[] bytes = Hex.decodeHex(s.toCharArray());

这篇关于十六进制编码的字符串到字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆