0

Here is the sample code in JS :

function toHexString(bytes) {
  return bytes.map(function(byte) {
      return ("00" + (byte & 0xFF).toString(16)).slice(-2);
    }).join('');
}

input -> Buffer.from("333138383223633D77DB", 'hex')
output -> 333138383223630770

Here is what I have tried so far in Python

def toHexString(byteArray):
    return ''.join('{:02x}'.format(x) for x in byteArray)


input -> bytearray.fromhex("333138383223633D77DB")
output -> 333138383223633d77db

I think the logic is correct but does not know what is wrong

My expectation result of the Python code should be similar to the result of JS code.

I would like to ask how should I update the python code to get the exact result as JS code

4
  • 5
    That JS code doesn't really make a lot of sense, the output is different from the input so toHexString doesn't do what its name suggests. Commented Jan 13, 2023 at 15:23
  • 3
    With "wrong" do you mean the JavaScript code or something in the Python code (in the latter case, explain what you expect instead)? Commented Jan 13, 2023 at 15:31
  • @ChauLoi but your JS code is broken, it does not output a hex string that is equal to the input. Commented Jan 13, 2023 at 15:45
  • The output shown (both variants) are similar. They are not however identical. What do you actually want? Commented Jan 13, 2023 at 15:53

3 Answers 3

1

Your Python code is correct (although it can be written much more concise as the other answers suggest), but your JS code isn't because it clearly outputs a hex string that is not the same as the input.

Instead, fix your JS code:

function toHexString(bytes) {
  return bytes.toString('hex').toUpperCase();
}

input -> Buffer.from("333138383223633D77DB", 'hex')
output -> 333138383223633D77DB

EDIT: if you really insist on Python code that outputs the same broken hex string, this may work:

import re

input  = '333138383223633D77DB';
output = ''
for m in re.finditer(r'..', input):
    match = m.group(0)
    output += match if re.match('[0-9][0-9]', match) else '0'
print(output)

(my Python skills are extremely rusty so it may not work for all inputs, and/or it can be written much more concise)

Sign up to request clarification or add additional context in comments.

3 Comments

I actually expect the response of 333138383223630770
@ChauLoi Please explain the logic behind that transformation
@Pingu I am working on this document github.com/loichau1997/Strega/blob/master/…
1

with python 3.5+ you can use hex()

def toHexString(byteArray):
    return byteArray.hex()

honestly do not think there is any need for defining any helper function when you can just run byteArray.hex()

2 Comments

Well, the question is that the Python code returns the same as the JS code, which it doesn't (because the JS code is broken).
hi I want to have the response of 333138383223630770
1

As has already been stated, there is built-in functionality for this in Python. However, if you insist on re-inventing the wheel then:

hs = '333138383223633D77DB'

def toHexString(ba):
    return ''.join([f'{b:02X}' for b in ba])

assert toHexString(bytearray.fromhex(hs)) == hs

Note the use of uppercase 'X' in the format specifier.

Also worth mentioning that bytearray.hex() returns a string in ASCII lowercase

1 Comment

hi I actually want to have the response of 333138383223630770

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.