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

  • Home
  • SEARCH
  • 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 64k
  • Answers 64k
  • 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
  • added an answer I found the issue - I had accidentially select the… May 11, 2026 at 10:41 am
  • added an answer The Animal class is the classic example of class inheritance… May 11, 2026 at 10:41 am
  • added an answer There is no need to explicitly unload modules in VB6.… May 11, 2026 at 10:41 am

Related Questions

In this thread some one commented that the following code should only be used
In this thread, we look at examples of good uses of goto in C
It seems like Groovy was forgotten in this thread so I'll just ask the
I had the same question as was asked in this thread , i.e. I
I am doing something like this in python class MyThread ( threading.Thread ): def
In other words, is this Singleton implementation thread safe: public class Singleton { private
In .NET, after this code, what mechanism stops the Thread object from being garbage
In this question , I asked about using queue's and threads in C#. I
This question is about using getter methods of a singleton object in worker threads.
In this question the answer was to flip on a switch that is picked

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.