How to convert a byte array to a hex string in Java?

后端 未结 27 3473
花落未央
花落未央 2020-11-21 04:19

I have a byte array filled with hex numbers and printing it the easy way is pretty pointless because there are many unprintable elements. What I need is the exact hexcode in

27条回答
  •  情书的邮戳
    2020-11-21 04:58

    If you're looking for a byte array exactly like this for python, I have converted this Java implementation into python.

    class ByteArray:
    
    @classmethod
    def char(cls, args=[]):
        cls.hexArray = "0123456789ABCDEF".encode('utf-16')
        j = 0
        length = (cls.hexArray)
    
        if j < length:
            v = j & 0xFF
            hexChars = [None, None]
            hexChars[j * 2] = str( cls.hexArray) + str(v)
            hexChars[j * 2 + 1] = str(cls.hexArray) + str(v) + str(0x0F)
            # Use if you want...
            #hexChars.pop()
    
        return str(hexChars)
    
    array = ByteArray()
    print array.char(args=[])
    

提交回复
热议问题