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

  • Home
  • SEARCH
  • 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 9013859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:22:22+00:00 2026-06-16T03:22:22+00:00

I have a Python project where I have a fixed byte-length text field (NOT

  • 0

I have a Python project where I have a fixed byte-length text field (NOT FIXED CHAR-LENGTH FIELD) in a comm protocol that contains a utf-8 encoded, NULL padded, NULL terminated string.

I need to ensure that a string fits into the fixed byte-length field. Since utf-8 is a variable width encoding, this makes using brute force to truncate the string at a fixed byte length dicey since you could possibly leave part of a multi-byte character dangling at the end.

Is there a module/method/function/etc that can help me with truncating utf-8 variable width encoded strings to a fixed byte-length?

Something that does Null padding and termination would be a bonus.

This seems like a nut that would have already been cracked. I don’t want to reinvent something if it already exists.

  • 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-16T03:22:23+00:00Added an answer on June 16, 2026 at 3:22 am

    Let Python detect and eliminate any partial or invalid characters.

    byte_str = uni_str.encode('utf-8')
    byte_str = byte_str[:size].decode('utf-8', 'ignore').encode('utf-8')
    

    This works because the UTF-8 spec encodes the number of following bytes in the first byte of a character, so the missing bytes can be easily detected.

    Edit: Here’s the results from this code using a random oriental character string I pulled from another question. The first number is the maximum size, the second is the actual number of bytes in the UTF-8 string.

    45 45 具有靜電產生裝置之影像輸入裝置
    44 42 具有靜電產生裝置之影像輸入裝
    43 42 具有靜電產生裝置之影像輸入裝
    42 42 具有靜電產生裝置之影像輸入裝
    41 39 具有靜電產生裝置之影像輸入
    40 39 具有靜電產生裝置之影像輸入
    39 39 具有靜電產生裝置之影像輸入
    38 36 具有靜電產生裝置之影像輸
    37 36 具有靜電產生裝置之影像輸
    36 36 具有靜電產生裝置之影像輸
    35 33 具有靜電產生裝置之影像
    34 33 具有靜電產生裝置之影像
    33 33 具有靜電產生裝置之影像
    32 30 具有靜電產生裝置之影
    31 30 具有靜電產生裝置之影
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a project that contains a library of Python and Scala packages, as
We have a python project that we want to start testing using buildbot. Its
I need to have an at-home project now that I'm working on Python/Django at
I have a python project that calls a c++ wrapper dll that calls a
I have a python project that calls a c++ dll that calls a c#
I have a Python project that has the following structure: package1 class.py class2.py ...
I have a python package that needs to pull in settings from my project
I have been learning Python on Codecademy.com and transferred a project that worked on
I have python project that runs on multiple machines. I am using virtualenv to
I have a Python project with many sub-modules that I package up with distutils.

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.