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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:41:09+00:00 2026-05-10T14:41:09+00:00

Below is my current char* to hex string function. I wrote it as an

  • 0

Below is my current char* to hex string function. I wrote it as an exercise in bit manipulation. It takes ~7ms on a AMD Athlon MP 2800+ to hexify a 10 million byte array. Is there any trick or other way that I am missing?

How can I make this faster?

Compiled with -O3 in g++

static const char _hex2asciiU_value[256][2] =      { {'0','0'}, {'0','1'}, /* snip..., */ {'F','E'},{'F','F'} };  std::string char_to_hex( const unsigned char* _pArray, unsigned int _len ) {     std::string str;     str.resize(_len*2);     char* pszHex = &str[0];     const unsigned char* pEnd = _pArray + _len;      clock_t stick, etick;     stick = clock();     for( const unsigned char* pChar = _pArray; pChar != pEnd; pChar++, pszHex += 2 ) {         pszHex[0] = _hex2asciiU_value[*pChar][0];         pszHex[1] = _hex2asciiU_value[*pChar][1];     }     etick = clock();      std::cout << 'ticks to hexify ' << etick - stick << std::endl;      return str; } 

Updates

Added timing code

Brian R. Bondy: replace the std::string with a heap alloc’d buffer and change ofs*16 to ofs << 4 – however the heap allocated buffer seems to slow it down? – result ~11ms

Antti Sykäri:replace inner loop with

 int upper = *pChar >> 4;  int lower = *pChar & 0x0f;  pszHex[0] = pHex[upper];  pszHex[1] = pHex[lower]; 

result ~8ms

Robert: replace _hex2asciiU_value with a full 256-entry table, sacrificing memory space but result ~7ms!

HoyHoy: Noted it was producing incorrect results

  • 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. 2026-05-10T14:41:09+00:00Added an answer on May 10, 2026 at 2:41 pm

    At the cost of more memory you can create a full 256-entry table of the hex codes:

    static const char _hex2asciiU_value[256][2] =     { {'0','0'}, {'0','1'}, /* ..., */ {'F','E'},{'F','F'} }; 

    Then direct index into the table, no bit fiddling required.

    const char *pHexVal = pHex[*pChar]; pszHex[0] = pHexVal[0]; pszHex[1] = pHexVal[1]; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 218k
  • Answers 218k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The classes that the DAO is to be injected into… May 12, 2026 at 11:30 pm
  • Editorial Team
    Editorial Team added an answer One way to find out: gcc -O1 -c -Q -v… May 12, 2026 at 11:30 pm
  • Editorial Team
    Editorial Team added an answer First, make sure you learn .NET (you're best off learning… May 12, 2026 at 11:30 pm

Related Questions

I am trying to explore OOP in C. I am however a C n00b
Trying to figure out why my list() class pointers are being overwritten with the
What I would like to get some input on is how to remove certain
I am trying to write a wrapper function to figure out who is calling

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.