w3resource

Hexadecimal values of list elements using memory views in Python


Write a Python program that creates a memory view from a list of integers and print the hex values of each element.

Sample Solution:

Code:

def test(memory_view):
    for element in memory_view:
        print(hex(element))

def main():
    nums = [8, 16, 42, 92, 128]
    print("Original list values:",nums)

    # Create a memory view from the list of integers
    memory_view = memoryview(bytearray(nums))

    print("Hex Values of said list elements:")
    test(memory_view)

if __name__ == "__main__":
    main()

Output:

Original list values: [8, 16, 42, 92, 128]
Hex Values of said list elements:
0x8
0x10
0x2a
0x5c
0x80

Flowchart:

Flowchart: Hexadecimal values of list elements using memory views in Python.

Previous: Slicing memory views in Python: Indexing syntax and example.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.