1

Following my first question How to convert an hexadecimale line in a text file to an array (Python)?, I need to convert a numpy file containing an hexadecimal array to a binary file, My numpy array looks like this:

[['7a', '8e', '5d', 'c3', '90', '78', '1e', 'ab', '8d', 'f2', 'c0', '90', 'bf',
  '4b', 'eb', 'ca'],
 ['db', 'ac', '0f', 'ba', '55', 'd3', 'd4', 'fc', '17', '71', '61', 'bf', 'e2',
  '4d', 'c7', 'fb'],
 ['82', 'e5', 'a7', 'a0', '21', '19', '7f', '6f', 'be', '94', 'a8', '67', 'd4',
  'bb', '38', '95'],
 ['85', '05', '80', 'c1', 'ff', 'ec', '88', '7c', '50', '00', 'c9', 'b3', 'a0',
  'e6', 'b3', '9d'],
 ['15', '26', 'af', '37', 'ce', '4b', '0b', '4e', '81', 'f8', 'af', '06', '47',
  'e3', '71', '19'],
 ['ba', 'b1', '9c', '53', 'fd', '86', 'e6', 'af', 'c9', '33', '27', '6b', '28',
  '6e', '0c', '36'],
 ['b5', '2e', '53', '00', '7b', 'eb', 'e8', '87', '7c', 'e5', '69', 'ad', '24',
  '94', 'dd', '76'],
 ['44', 'fd', '87', 'e9', 'b1', 'a4', '0a', '92', '9a', 'b6', '13', '56', '65',
  'c2', '2d', '0f'],
 ['a8', '8e', '51', '41', 'dd', 'c9', '9e', '02', '07', 'a9', 'e1', '44', 'f4',
  '01', '0a', '22'],
 ['58', 'ff', '59', '78', '19', 'ea', '0a', 'a3', '70', '24', 'a1', 'f1', 'f8',
  '4c', '52', '24']]

I try by using this code:

import numpy as np
import struct
textin = np.load('/home/msmache/Bureau/OutFilePlaintexts.npy')
f = open('/home/msmache/Bureau/Plaintexts', 'wb')
for t in textin.flatten():
    f.write(struct.pack('h', t))
f.close()

I found this error:

f.write(struct.pack('h', t))
error: pack expected 3 items for packing (got 1)
2

0

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.