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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:39:00+00:00 2026-06-11T18:39:00+00:00

This is my first cython (pyx) module. How can I make this faster? Specifically

  • 0

This is my first cython (pyx) module. How can I make this faster? Specifically I’m looking for help on the last line. It compiles and runs, but I am concerned it is transformed into Py* object stuff, which could probably be much faster.

Also, if you see any obvious errors, please let me know!

ctypedef unsigned short UInt8

DEF BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"

def encode_16_bytes(char *thebytes):
    cdef UInt8 *b      # the bytes cast to an array of UInt8
    cdef int i         # used to assign a value to the buffer
    cdef int j         # used to count up through the bytes
    cdef char buf[23]  # the resulting buffer of characters

    # initialize variables
    for i in range(23): # set buf array to zeros
        buf[i] = '\0'
    b = <UInt8 *>thebytes
    i = 0
    j = 1

    i += 1
    buf[i] = BASE64[(b[0] >> 6) & 0x3F]
    i += 1
    buf[i] = BASE64[b[0] & 0x3F]

    # iterate through the bytes 4 words at a time, setting each byte to its
    # mapped BASE64 counterpart
    while j < 16:
        i += 1
        buf[i] = BASE64[(b[j] >> 2) & 0x3F]
        i += 1
        buf[i] = BASE64[((b[j] << 4) | (b[j + 1] >> 4)) & 0x3F]
        i += 1
        buf[i] = BASE64[((b[j + 1] << 2) | (b[j + 2] >> 6)) & 0x3F]
        i += 1
        buf[i] = BASE64[b[j + 2] & 0x3F]
        j += 3

    # join up the characters into a string
    return "".join([chr(buf[i]) for i in range(23)])

Thanks!

  • 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-11T18:39:01+00:00Added an answer on June 11, 2026 at 6:39 pm

    You can create a Python bytes string out of the C char buffer by replacing the “”.join(…) with the following:

    bytes_string = chr[:23]
    

    Or more generally:

    bytes_string = chr[:buffer_length]
    

    See here for reference and more examples.

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

Sidebar

Related Questions

Please Help me to do this first i have this div <div class=loader alt=stat.php>
Asked this first on serverfault, and someone recommended that I ask here. I'm looking
I have some json code like this: { First name: David, Last name: Esseiva
Can I combine two queries like this first: UPDATE table SET col1=1 WHERE id='x';
I have a textfile.txt like this: First Line Second Line Third Line Fourth Line
give a JSON data like this { name: { firstname: 'First Name', lastname: 'Last
This file is an output file whose first line is a header followed by
Please read this first. How do I make a button invisible just after click?
You can actually just skip this first part cause it gets confusing, but I'll
I am doing this first time so I need some help. I am using

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.