如何将hexstring转换为(hex)字节

时间:2017-03-30 01:15:42

标签: java hex

我有一个像#34; 0xFF"并希望将字符串转换为字节0xFF,因为另一个函数需要将值作为十六进制。所以像这样:

         String hexstring="0xFF";
         //convert to byte
         byte hexbyte = (byte) 0xFF;

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

<!DOCTYPE html> <html ng-app="plunker"> <head> <meta charset="utf-8" /> <title>AngularJS Plunker</title> <script> document.write('<base href="' + document.location + '" />'); </script> <link rel="stylesheet" href="style.css" /> <script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script> <script src="app.js"></script> </head> <body> <div ng-controller="Ctl1"> <p>Hello {{name}}!</p> </div> <div ng-controller="Ctl2"> <button ng-click="changeValue()">Change Value</button> </div> </body> </html>将为您效劳

答案 1 :(得分:0)

public static byte[] asByteArray(String hex) {
    // Create a byte array half the length of the string.
    byte[] bytes = new byte[hex.length() / 2];

    // Repeat the process for the number of elements in the byte array.
    for (int index = 0; index < bytes.length; index++) {
        // Convert hexadecimal string to bytes and store in array.
        bytes[index] =
            (byte) Integer.parseInt(
                hex.substring(index * 2, (index + 1) * 2),
                16);
    }

    // return byte array。
    return bytes;
}