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 8929831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:45:31+00:00 2026-06-15T08:45:31+00:00

I’ve been trying to debug this for far too long, and I obviously have

  • 0

I’ve been trying to debug this for far too long, and I obviously have no idea what I’m doing, so hopefully someone can help. I’m not even sure what I should be asking, but here it goes:

I’m trying to send Apple Push Notifications, and they have a payload size limit of 256 bytes. So subtract some overhead stuff, and I’m left with about 100 english characters of main message content.

So if a message is longer than the max, I truncate it:

MAX_PUSH_LENGTH = 100
body = (body[:MAX_PUSH_LENGTH]) if len(body) > MAX_PUSH_LENGTH else body

So that’s fine and dandy, and no matter how long of a message I have (in english), the push notification sends successfully. However, now I have an Arabic string:

str = "هيك بنكون 
عيش بجنون تون تون تون هيك بنكون 
عيش بجنون تون تون تون 
أوكي أ"

>>> print len(str)
109

So that should truncate. But, I always get an invalid payload size error! Curious, I kept lowering the MAX_PUSH_LENGTH threshold to see what it would take for it to succeed, and it’s not until I set the limit to around 60 that the push notification succeeded.

I’m not exactly sure if this has something to do with the byte size of languages other than english. It is my understanding that an English character takes one byte, so does an Arabic character take 2 bytes? Might this have something to do with it?

Also, the string is JSON encoded before it is sent off, so it ends up looking something like this: \u0647\u064a\u0643 \u0628\u0646\u0643\u0648\u0646 \n\u0639\u064a\u0634 ... Could it be that it is being interpreted as a raw string, and just u0647 is 5 bytes?

What should I be doing here? Are there any obvious errors or am I not asking the right question?

  • 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-15T08:45:32+00:00Added an answer on June 15, 2026 at 8:45 am

    You need to cut to bytes length, so you need first to .encode('utf-8') your string, and then cut it at a code point boundary.

    In UTF-8, ASCII (<= 127) are 1-byte. Bytes with two or more most significant bits set (>= 192) are character-starting bytes; the number of bytes that follow is determined by the number of most significant bits set. Anything else is continuation bytes.

    A problem may arise if you cut the multi-byte sequence in the middle; if a character did not fit, it should be cut completely, up to the starting byte.

    Here’s some working code:

    LENGTH_BY_PREFIX = [
      (0xC0, 2), # first byte mask, total codepoint length
      (0xE0, 3), 
      (0xF0, 4),
      (0xF8, 5),
      (0xFC, 6),
    ]
    
    def codepoint_length(first_byte):
        if first_byte < 128:
            return 1 # ASCII
        for mask, length in LENGTH_BY_PREFIX:
            if first_byte & mask == mask:
                return length
        assert False, 'Invalid byte %r' % first_byte
    
    def cut_to_bytes_length(unicode_text, byte_limit):
        utf8_bytes = unicode_text.encode('UTF-8')
        cut_index = 0
        while cut_index < len(utf8_bytes):
            step = codepoint_length(ord(utf8_bytes[cut_index]))
            if cut_index + step > byte_limit:
                # can't go a whole codepoint further, time to cut
                return utf8_bytes[:cut_index]
            else:
                cut_index += step
        # length limit is longer than our bytes strung, so no cutting
        return utf8_bytes
    

    Now test. If .decode() succeeds, we have made a correct cut.

    unicode_text = u"هيك بنكون" # note that the literal here is Unicode
    
    print cut_to_bytes_length(unicode_text, 100).decode('UTF-8')
    print cut_to_bytes_length(unicode_text, 10).decode('UTF-8')
    print cut_to_bytes_length(unicode_text, 5).decode('UTF-8')
    print cut_to_bytes_length(unicode_text, 4).decode('UTF-8')
    print cut_to_bytes_length(unicode_text, 3).decode('UTF-8')
    print cut_to_bytes_length(unicode_text, 2).decode('UTF-8')
    
    # This returns empty strings, because an Arabic letter
    # requires at least 2 bytes to represent in UTF-8.
    print cut_to_bytes_length(unicode_text, 1).decode('UTF-8')
    

    You can test that the code works with ASCII as well.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have been unable to fix a problem with Java Unicode and encoding. The
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string

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.