Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7935209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:51:11+00:00 2026-06-03T21:51:11+00:00

I have a small script here that I need some help with: for song

  • 0

I have a small script here that I need some help with:

for song in songs:
    slash = song.rindex('\\')
    songbyte = slash + 2
    if len(str(songbyte)) < 2:
    #if songbyte < 10:
        songbyte = "0" + str(songbyte)
        f.write(binascii.a2b_hex(songbyte))
    else:
        f.write(binascii.a2b_hex("{0:x}".format(songbyte)))
f.close()
return

First off, I’m attempting to write a playlist. For testing purposes I’m writing one that contains 49 songs — songs is a list that i’m iterating through containing 49 songs

I’m required to write songbyte — It’s a value that ranges from 8 to 72. At the moment I’m just iterating through and making sure it has the correct value for each songbyte (It’s a copy of a known correct playlist, veryifying via diff and a hex editor).

My problem is that the line

print(binascii.a2b_hex("{0:x}".format(songbyte)))

throws “TypeError: Odd-length string”. Now, this is descriptive enough. Through investigating, I’ve determined that the value of songbyte when it’s going to error is 8. What doesn’t make sense, however, is that this error happens 37 songs into the list, and the majority of the the other songs’ songbytes are also 8 — and get caught with my len(str(songbyte)) < 2 check which adds a 0 — but strangely this one doesn’t.

Though I hope, I’m not really sure if this is enough information to help me solve the problem, short of providing complete details of the whole script though I don’t think I could. Is there another way that I can write songbyte — as hex — to a file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:51:12+00:00Added an answer on June 3, 2026 at 9:51 pm

    You should use struct.pack, which is meant exactly for this purpose:

    import struct
    for song in songs:
        slash = song.rindex('\\')
        songbyte = slash + 2
        f.write(struct.pack('!B', songbyte))
    

    The format string "{0:x}" will not insert zeroes before the number, you’d want "{0:02x}".

    >>> "{0:x}".format(12)
    'c'
    >>> "{0:02x}".format(12)
    '0c'
    

    Nevertheless, using binascii leads to unnecessarily complex and brittle code. For example, it would silently generate more than 2 characters if the value exceeds 255.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, here is the question. I have a small flash application that sends some
I have a small script that I am try to port to work for
I have following small script to preview some text before submitting it to store
I have a small script that reads a file. After reading a line i'm
I have a small cgi script that fetches and validates a configuration file for
I have this small PHP script that takes a POST variable and writes it
I have this nifty little script that does a nice job of manipulating some
So I have a small PHP script that generates an xml feel for the
I seem to have a small issue with my script where I need to
I have a small script that launches and, every half hour, feeds a command

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.