Hacker News new | ask | show | jobs
by throwawayish 3450 days ago
Indeed. For this use case to_bytes would be correct (writing this in the most verbose way possible :)

    >>> (-352).to_bytes(length=4, byteorder='big', signed=True).hex()
    'fffffea0'
    >>> (352).to_bytes(length=4, byteorder='big', signed=True).hex()
    '00000160'
Note how well-defined and independent of the actual machine this conversion is. Since you define everything - length, byte order and whether to get two's complement or not - you'll get the same output everywhere.