Go Back   The UNIX and Linux Forums > Top Forums > Programming
google site



Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Search this Thread
  #1  
Old 4 Weeks Ago
Registered User
 

Join Date: Mar 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Python3 bytearray padding

Hi,
I'm trying to create a padded bytearray in Python3.
Currently I have something like this:


Code:
inputstuff = [
('A' , b'ABCD'),
('B' , b'EFGH'),
('C' , b'IJKL'),
('D' , b'MNOP')
]
BA = bytearray()

for thing in inputstuff:
        BA.extend(thing[1])
 
print(BA.decode('latin1'))
print(BA)
 
for thing in BA:
        print(thing)

This works and gives output like this:


Code:
ABCDEFGHIJKLMNOP
bytearray(b'ABCDEFGHIJKLMNOP')
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

Which is expected.

However, what i'd actually like to do is pad the bytes I add to the bytearray with blanks such that it would be more like this:

ABCD EFGH IJKL MNOP

Obviously I could just add spaces to the end of each string defined as A, B, C and D - but i'd rather not do that as it would get complicated when I use this code in a real scenario where these are objects being passed into the function etc.

If anybody knows how to do this i'd be very grateful of your help.

Regards,

Phil

Last edited by pludi; 4 Weeks Ago at 08:16 AM.. Reason: code tags, please...
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Padding with zeros. sbasetty Shell Programming and Scripting 10 05-18-2009 07:09 PM
How to convert byteArray variables to HexaString variables for Linux? ritesh_163 Programming 2 08-10-2008 11:55 PM
Padding in Unix rudoraj Shell Programming and Scripting 3 02-20-2008 11:06 PM
Padding vijaygopalsk UNIX for Dummies Questions & Answers 2 06-27-2003 10:51 AM
Padding issues informshilpa UNIX for Advanced & Expert Users 2 03-01-2002 12:51 PM



All times are GMT -4. The time now is 09:22 PM.