Bytearray from hex python

How to Convert Hex String to Bytes in Python?

Data is often represented in hexadecimal format, which is a commonly used system for representing binary data in a human-readable form. In Python, the hex string is converted into bytes to work with binary data or to extract each value from the raw data. To convert the hex string into bytes, the bytes.from() and codecs.decode() functions are used.

This post provides various ways to convert hex string to bytes using the given below contents:

Method 1: Using bytes.fromhex(hex_string)

The “bytes.fromhex()” function is used to return a byte object (sequences of integers) by accepting the single hexadecimal value. The “bytes.fromhex(hex_string)” function is utilized in the following code to convert hex string to bytes:

hex_string = 'ff' print('Hex String Value: ',hex_string) output = bytes.fromhex(hex_string) print('Bytes Value: ',output) print(type(output))

In this example, the “bytes.fromhex()” function takes the hexadecimal strings “ff” as an argument and converts it into bytes object.

The given hex string has been converted into bytes.

Method 2: Using codecs.decode() Function

The “codecs.decode()” function is used to decode binary strings into normal form. In the below code the “codecs.decode()‘ function is used to convert hex string to bytes:

import codecs bytes_value = codecs.decode('ff', 'hex_codec') print(type(bytes_value)) print(bytes_value)

The “codecs.decode()” function converts the hex string into bytes by taking the hex string “ff” as a first argument and the encoding “hex_codec” as a second argument.

The given string has been converted into a bytes object.

Method 3: Using the binascii Module

In Python, the module named “binascii” is utilized to convert the binary representations into ASCII-encoded binary representations. The “binascii.unhexlify()” function of the binascii module is used in the below code to convert hex string to bytes:

import binascii hexa_string = '507974686f6e' bytes_value = binascii.unhexlify(hexa_string) print('Byte value of the string:', bytes_value)

The “binascii.unhexlify()” function takes the “hex string” as an argument and converts them into bytes.

The input hex string “507974686f6e” has been converted into bytes “Python.”

Читайте также:  Width full on screen css

Conclusion

To convert hex strings to bytes, the “bytes.fromhex()” and “codecs.decode()” functions are used. Apart from that, the “binascii” module is also exercised in Python. The “bytes.fromhex()” function takes the hex string as an argument and converts it into bytes. Similarly, the “codecs.decode()” function decodes the hex string into bytes, and the “unhexlify()” function can also return the bytes of the input hex string. This guide presented various ways to convert hex strings to bytes in Python.

Источник

Python – Hex String to Bytearray

Be on the Right Side of Change

Hex String to Bytearray using bytearray.fromhex(hex_string)

To convert a hexadecimal string to a bytearray object, pass the string as a first argument into bytearray.fromhex(hex_string) method. For example, bytearray.fromhex(‘ff’) yields bytearray(b’\xff’) .

hex_string = 'ff' print(bytearray.fromhex(hex_string)) # bytearray(b'\xff')

Examples

And here’s how you can convert the additional examples shown above:

>>> bytearray.fromhex('01') bytearray(b'\x01') >>> bytearray.fromhex('0101') bytearray(b'\x01\x01') >>> bytearray.fromhex('04') bytearray(b'\x04') >>> bytearray.fromhex('01 0a') bytearray(b'\x01\n') >>> bytearray.fromhex('01 02 0e 0f 0f') bytearray(b'\x01\x02\x0e\x0f\x0f') >>> bytearray.fromhex('0f0f') bytearray(b'\x0f\x0f') >>> bytearray.fromhex('ff') bytearray(b'\xff')

Convert Hex String with Prefix ‘0x’ to Bytearray

If your hex string has a prefix ‘0x’ in front of it, you can convert it into a bytearray object by using slicing operation hex_string[2:] to get rid of the prefix before converting it using bytearray.fromhex(hex_string[2:]) .

hex_string = '0x0f' print(bytearray.fromhex(hex_string[2:])) # bytearray(b'\x0f')

If you don’t know slicing well enough in Python, feel free to check out my in-depth tutorial here:

A simple way to convert a hexadecimal string hex_string to a bytes type is to use the bytes.fromhex(hex_string) method. For example, bytes.fromhex(‘deadbeef’) returns the b’\xde\xad\xbe\xef’ type.

hex_string = 'deadbeef' print(bytes.fromhex(hex_string)) # b'\xde\xad\xbe\xef'

After reading this, you may wonder:

What’s the Difference Between bytearray and bytes?

The difference between bytearray and bytes types is that bytes is an immutable version of bytearray . So you can modify an object of the latter but not of the former.

For example, let’s create both types from the same hex_string as learned before:

hex_string = 'deadbeef' my_bytes = bytes.fromhex(hex_string) my_bytearray = bytearray.fromhex(hex_string)

Next, you’ll change the my_bytearray variable — no problem:

print(my_bytearray) # bytearray(b'\xde\xad\xbe\xef') # Modify bytearray is possible my_bytearray[0] = 3 print(my_bytearray) # bytearray(b'\x03\xad\xbe\xef')

But what happens if you try to modify the my_bytes variable? An error TypeError: ‘bytes’ object does not support item assignment !

>>> my_bytes[0] = 3 Traceback (most recent call last): File "", line 1, in my_bytes[0] = 3 TypeError: 'bytes' object does not support item assignment

To fix this error, don’t change the bytes type in your code snippet or use a mutable bytearray instead of an immutable bytes type.

Where to Go From Here

Thanks for reading through the whole tutorial—I’m happy and grateful that you visited us on the Finxter blog because my mission is to help coders like you reach their goals faster.

Читайте также:  Html business card templates

I believe in the transformative power we coders have to change the world and make it more efficient!

If you want to keep improving your skills, feel free to check out my free email academy and download Python Cheat Sheets here:

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.

Be on the Right Side of Change 🚀

  • The world is changing exponentially. Disruptive technologies such as AI, crypto, and automation eliminate entire industries. 🤖
  • Do you feel uncertain and afraid of being replaced by machines, leaving you without money, purpose, or value? Fear not! There a way to not merely survive but thrive in this new world!
  • Finxter is here to help you stay ahead of the curve, so you can keep winning as paradigms shift.

Learning Resources 🧑‍💻

⭐ Boost your skills. Join our free email academy with daily emails teaching exponential with 1000+ tutorials on AI, data science, Python, freelancing, and Blockchain development!

Join the Finxter Academy and unlock access to premium courses 👑 to certify your skills in exponential technologies and programming.

New Finxter Tutorials:

Finxter Categories:

Источник

Convert Hex to Byte in Python

Convert Hex to Byte in Python

  1. Initialize a Hexadecimal Value
  2. Use bytes.fromhex() to Convert Hex to Byte in Python
  3. Use the unhexlify Module to Convert a Hex to Byte in Python

This tutorial will introduce how to convert hexadecimal values into a byte literal in Python.

Hexadecimal or hex values is a representation of a number made up of 16 symbols instead of the usual 10 symbols in decimal values. The numbers from 0 until 9 and the characters a until e makes up the 16 hex symbols.

Читайте также:  Apache убрать index php

For example, the hexadecimal conversion for the number 1000 is 3E8 .

Initialize a Hexadecimal Value

Let’s create a hexadecimal value using a string and convert the phrase A quick brown fox into a hex value using the function hexlify() that is a function in the binascii module.

Also, to convert a string into a hex, we would need to convert the string into a byte to be able to convert it into hex.

import binascii str_val = 'A quick brown fox'.encode('utf-8') hex_val = binascii.hexlify(str_val).decode('utf-8')  print(hex_val) 
4120717569636b2062726f776e20666f78 

Now we’ve successfully converted a string into hex, let’s proceed on how to convert a hex into a byte.

Use bytes.fromhex() to Convert Hex to Byte in Python

The function bytes.fromhex() accepts a single hexadecimal value argument and converts it into a byte literal.

Taking the hex value from the previous result, use fromhex() to convert it into a byte literal.

hex_val = '4120717569636b2062726f776e20666f78'  print(bytes.fromhex(hex_val)) 
Byte value: b'A quick brown fox' 

The result will output the bytes literal, which is the phrase converted into a hex prefixed with the letter b to specify that the value is a byte literal.

Use the unhexlify Module to Convert a Hex to Byte in Python

The binascii Python module contains efficient utility functions for binary and ASCII operations. unhexlify() is a function within the binascii module that converts a hexadecimal value into a byte literal.

Let’s initialize a new example with special non-ASCII characters, which will then be converted into a hexadecimal value. The example will be the Greek translation of the phrase a quick brown fox .

import binascii from binascii import unhexlify  str_val = 'Μια γρήγορη καφέ αλεπού'.encode('utf-8') #A quick brown fox in Greek translation hex_val = binascii.hexlify(str_val).decode('utf-8')  print('String value: ', str_val.decode('utf-8')) print('Hexadecimal: ', hex_val) print('Byte value: ', unhexlify(hex_val)) 
String value: Μια γρήγορη καφέ αλεπού Hexadecimal: ce9cceb9ceb120ceb3cf81ceaeceb3cebfcf81ceb720cebaceb1cf86cead20ceb1cebbceb5cf80cebfcf8d Byte value: b'\xce\x9c\xce\xb9\xce\xb1 \xce\xb3\xcf\x81\xce\xae\xce\xb3\xce\xbf\xcf\x81\xce\xb7 \xce\xba\xce\xb1\xcf\x86\xce\xad \xce\xb1\xce\xbb\xce\xb5\xcf\x80\xce\xbf\xcf\x8d' 

We have now successfully converted hexadecimal values to bytes.

In this article, we’ve covered using fromhex() and binascii.unhexlify() to convert hexadecimal values to byte literals in Python. fromhex() is preferable if you don’t want added imports into your source code. Otherwise, both functions will provide the same output.

Skilled in Python, Java, Spring Boot, AngularJS, and Agile Methodologies. Strong engineering professional with a passion for development and always seeking opportunities for personal and career growth. A Technical Writer writing about comprehensive how-to articles, environment set-ups, and technical walkthroughs. Specializes in writing Python, Java, Spring, and SQL articles.

Related Article — Python Bytes

Related Article — Python Hex

Источник

Оцените статью