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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:56:44+00:00 2026-05-15T19:56:44+00:00

I need to generate a path string from a number (in C) e.g: 53431453

  • 0

I need to generate a path string from a number (in C)

e.g:

53431453 -> 0003/2F4/C9D

what I have so far is this:

char *id_to_path(long long int id, char *d)
{
    char t[MAX_PATH_LEN];
    sprintf(t, "%010llX", id);

    memcpy(d,   t,   4);
    memcpy(d+5, t+4, 3);
    memcpy(d+9, t+7, 4);

    d[4] = d[8] = '/';

    return d;
}

I’m wondering if there’s a better way, e.g to generate the final string in one step instead of doing sprintf and then moving the bytes around.

Thanks

Edit:

I benchmarked the given solutions

results in operations per second (higher is better):

(1) sprintf + memcpy  : 3383005
(2) single sprintf    : 2219253 
(3) not using sprintf : 10917996

when compiling with -O3 the difference is even greater:

(1) 4422101
(2) 2207157
(3) 178756551

Since this function will be called a lot, I’ll use the fastest solution even though the single sprintf is the shortest and most readable.

Thanks for your answers!

  • 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-05-15T19:56:45+00:00Added an answer on May 15, 2026 at 7:56 pm

    Since the string uses hex, it can be quite easily done using shift and bit operators.

    Getting the 4 highest bits from the value can be done like this:

    id >> 28
    

    Converting this to a digit simply means adding the character ‘0’ to it, like this:

    '0' + (id >> 28)
    

    However, since A, B, C, … don’t immediately follow the character 9, we have to perform an additional check, something like:

    if (c > '9') c = c - '9' - 1 'A'
    

    If we want the next 4 bits, we should only shift 24 bits, but then we still have the highest 4 bits left, so we should mask them out, like this:

    (id >> 24) & 0xf
    

    If we pour this into your function, we get this:

    char convert (int value)
    {
    char c = value + '0';
    if (c > '9') c = c - '9' - 1 + 'A';
    return c;
    }
    
    void main()
    {
    long id = 53431453;
    char buffer[20];
    
    buffer[0] = convert(id >> 28);
    buffer[1] = convert((id >> 24) & 0xf);
    buffer[2] = convert((id >> 20) & 0xf);
    buffer[3] = convert((id >> 16) & 0xf);
    buffer[4] = convert((id >> 12) & 0xf);
    buffer[5] = convert((id >>  8) & 0xf);
    buffer[6] = convert((id >>  4) & 0xf);
    buffer[7] = convert((id >>  0) & 0xf);
    buffer[8] = '\0';
    }
    

    Now adjust this to add the slashes in between, the extra zeroes in the beginning, …

    EDIT:
    I know this is not in one step, but it is better extensible if you later want to change the places of the slashes, …

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

Sidebar

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.