将字节数组转换为hexstring时 [英] While converting byte array to hexstring

查看:87
本文介绍了将字节数组转换为hexstring时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我从google获得了一段代码,我希望将字节数组(随机生成)转换为十六进制字符串。我知道很少的线条代码..可以任何人帮忙。??



我使用的三个功能:



hi All,

I got a piece of code from google where i want to convert a byte array(randomly generated) to hex string. i dint understand few code of lines..can any one help please.??

The three functions i used :

public static String CellKeysGeneration() {

        byte[] btba = new byte[5];

        Random r = new Random();

        r.nextBytes(btba);

        for (int i = 0; i < btba.length; i++) {

            btba[i] = btba[i];

        }
        String str = tohexString(btba);
        return str;
    }

    public static String tohexString(byte[] bytes) {

        StringBuffer sb = new StringBuffer(bytes.length * 2);

        for (int i = 0; i < bytes.length; i++) {
            sb.append(tohex(bytes[i] >> 4));   
            sb.append(tohex(bytes[i]));

        }

        return sb.toString();

    }

    public static char tohex(int nibble) {

        final char[] hexdigit = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                '9', 'A', 'B', 'C', 'D', 'E' , 'F' };

        return hexdigit[nibble & 0xF];
    }





1)我不明白为什么总是只用0xF进行和操作。

2)在tohexString()中,为什么>>用4操作完成?



任何人都可以帮助我理解tohexString()和toHex()方法:)



1) I din''t understand why always "and" operation is done only with 0xF.
2) In tohexString(), why ">>" operation with 4 is done?

Can any one help me to understand tohexString() and toHex() methods :)

推荐答案



  1. 因为传递给 tohex()方法的值总是4位值。
  2. 提取字节最左边的4位并将其传递给 tohex()方法。

  1. Because the value passed to the tohex() method is always a 4-bit value.
  2. To extract the leftmost 4 bits of the byte and pass it to the tohex() method.



如果您单步执行代码,它会像:



  • 按顺序选择下一个字节
  • 右移4位并将得到的4位(左半部分)传递给 tohex()方法
  • 将结果字符追加到字符串
  • 将此字节传递给 tohex()方法,该方法屏蔽右半部分
  • 将结果字符附加到字符串

  • If you step through the code it goes something like:


    • Select the next byte in sequence
    • Right shift 4 bits and pass the resulting 4 bits (the left half) to the tohex() method
    • Append the resulting character to the string
    • Pass this byte to the tohex() method which masks off the right half
    • Append the resulting character to the string

    • 引用:

      1)我不明白为什么总是和只使用0xF进行操作。

      1) I din''t understand why always "and" operation is done only with 0xF.

      这样的操作掩盖(丢弃) 4 字节的最高有效位,因为 0xF 以二进制表示形式 00001111 。 (例如 10101001& 00001111 = 00001001 )。





      Such operation masks out (discards) the 4 most significant bits of the byte, because 0xF is 00001111 in binary representation. (e.g. 10101001 & 00001111 = 00001001).


      引用:

      2)在tohexString()中,为什么>>完成4的操作?

      2) In tohexString(), why ">>" operation with 4 is done?

      这个''将右半字节移动到左边',即 4 字节的最高有效位替换 4 最不重要的(右移运算符>> 向右移动位)。因此,例如, 0xAB 变为 0x0A

      This ''moves the right nibble to the left'', that is the 4 most significant bits of the byte replace the 4 least significant ones (the right shift operator >> moves bits to the right). Hence, for instance, 0xAB becomes 0x0A.


      这篇关于将字节数组转换为hexstring时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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