0

I wrote in Java in the following code. I've tried a lot of methods to translate it into Python. But I don't get the result I want.

These are the Java code and Python translation. Where am I making a mistake?

public static String veri(byte[] bArr){    
    char[] toCharArray = "0123456789abcdef".toCharArray();
    char[] cArr = new char[(bArr.length * 2)];

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

        int i2 = bArr[i] & 255;

        cArr[i * 2] = toCharArray[i2 >>> 4];
        cArr[(i * 2) + 1] = toCharArray[i2 & 15];
    }

    //
    return new String(cArr);
}
def x(sig):
    sig = bytearray(sig.encode())
    char_string = "0123456789abcdef"

    char_list = list(char_string)

    charrr = bytearray(sig.__len__() * 2)

    for key in range(sig.__len__()):
        i2 = sig[key] & 255
        charrr[key * 2] = (char_list[i2 >> 4])
        charrr[(key * 2) + 1] = (char_list[i2 & 15])

    print(charrr.decode())
    return (charrr)
unlut
  • 3,525
  • 2
  • 14
  • 23
  • Hi there. It would be easier to help if you could edit the question to tell us what you expect the code to do, and what it actually _is_ doing instead. Thanks! – holdenweb Feb 16 '19 at 10:34
  • What output do you get in python code and what output do you get in java code? Did you try to debug the python code? – Chetan Feb 16 '19 at 10:35
  • i need convert to python this code char[] toCharArray = "0123456789abcdef".toCharArray(); char[] cArr = new char[(bArr.length * 2)]; – Tolga Taşçı Feb 16 '19 at 10:35
  • return response full numeric wrong string and numeric "0123456789abcdef" operator >>> java i not find python operator >>> – Tolga Taşçı Feb 16 '19 at 10:36
  • Add what you are expecting, some outputs from both methods, what's wrong according to you... don't expect us to do all your work for you. – josepdecid Feb 16 '19 at 10:38
  • Did you research about how to do logical shift in python? Did you find [this](https://stackoverflow.com/questions/5832982/how-to-get-the-logical-right-binary-shift-in-python) – Chetan Feb 16 '19 at 10:38
  • This might be an XY problem: I think the real question is "How do I build a packed decimal byte string in Python." While by no means a complete answer [this article](https://www.linuxtopia.org/online_books/programming_books/python_programming/python_ch34s05.html) gives a few clues. – holdenweb Feb 16 '19 at 10:40
  • 1
    `I wrote in Java in the following code`. I don't think so. I googled `cArr[i * 2] = [i2 >>> 4]`, and the first result looks like this: https://ibb.co/X8hsgmg . For some reason the file doesn't open, but the content clearly matches yours. –  Feb 16 '19 at 10:45

0 Answers0