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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:27:49+00:00 2026-05-11T01:27:49+00:00

In this thread some one commented that the following code should only be used

  • 0

In this thread some one commented that the following code should only be used in ‘toy’ projects. Unfortunately he hasn’t come back to say why it’s not of production quality so I was hoping some one in the community may be able to either assure me the code is ok (because I quite like it) or identify what is wrong.

template< class T1, class T2> void hexascii( T1& out, const T2& in ) {     out.resize( in.size() * 2 );     const char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7','8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};     T1::iterator outit = out.begin();     for( T2::const_iterator it = in.begin(); it != in.end(); ++it )     {         *outit++ = hexDigits[*it >> 4];         *outit++ = hexDigits[*it & 0xF];     } }  template<class T1, class T2> void asciihex( T1& out, const T2& in ) {     size_t size = in.size;     assert( !(size % 2) );      out.resize( size / 2 );     T1::iterator outit = out.begin();     for( T2::const_iterator it = in.begin(); it != in.end(); it += 2, ++outit )     {     *outit = ((( (*it > '9' ? *it - 0x07 : *it)  - 0x30) << 4) & 0x00f0) +                  (((*(it+1) > '9' ? *(it+1) - 0x07 : *(it+1)) - 0x30) & 0x000f);     } } 

Edit: Thanks for your help guys, you’ve made some big improvements. I’ve written functions in the two suggested styles from your answers. Some rough testing suggests the second method is marginally faster than the first, but IMO this is outweighed by the improved readability of the first.

template<class T1> void asciihex2( T1& out, const std::string& in ) {     dassert( sizeof(T1::value_type)==1 );     size_t size = in.size(); assert( !(size % 2) );     out.resize( size / 2 );     T1::iterator outit = out.begin();     for( size_t i = 0; i < in.size(); i += 2 )     {         int tmp;         sscanf( in.c_str() + i, '%02X', &tmp );         *outit++ = tmp;     } }  template<class T1> void asciihex3( T1& out, const std::string& in ) {     dassert( sizeof(T1::value_type)==1 );     size_t size = in.size(); assert( !(size % 2) );     out.resize( size / 2 );     T1::iterator outit = out.begin(); const char hexDigits[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,                            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,                   0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; for( std::string::const_iterator it = in.begin(); it != in.end(); it += 2, ++outit )     {     *outit = (hexDigits[(*it - 0x30) & 0x1f] << 4) +                hexDigits[((*(it+1) - 0x30) & 0x1f)];     } } 

Some of the assumptions surronding this code: 1: They are not intended as a generic, but are used in an anonymous name space to translate data for a specific class. 2: The templating is required as two separate container types are being used (one being std::vector, the other a similar byte array type container from a third party library. 3: The purpose is to be able to convert binary data of indeterminate length into strings and back again (0x1234abcd <-> ‘1234abcd’) 4: assert traps errors in both debug and release modes 5: by the time these functions are called the size of the string will already have been checked, assert is used to terminate processing if something serious has gone wrong 6: It needs some commenting

Any other ideas appreciated.

  • 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-11T01:27:50+00:00Added an answer on May 11, 2026 at 1:27 am

    It seems like a lot of templated code to achieve very little, given you have direct hex conversion in the standard C scanf and printf functions. why bother?

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer While there aren't any workable formulas to help you calculate… May 11, 2026 at 11:30 pm
  • Editorial Team
    Editorial Team added an answer The graph does not show up in when running a… May 11, 2026 at 11:30 pm
  • Editorial Team
    Editorial Team added an answer You can use a Derived Column transform. I don't have… May 11, 2026 at 11:30 pm

Related Questions

I'm getting this error: Cannot use a DependencyObject that belongs to a different thread
I've done a series of questions about J2ME Game developing, and in a recent
I'm working on a JavaScript application that has to perform two separate checks via
Perhaps my question is similar in nature to this one: Do you use design

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.