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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:18:54+00:00 2026-05-31T00:18:54+00:00

I want to build a function to easily convert a string containing hex code

  • 0

I want to build a function to easily convert a string containing hex code (eg. “0ae34e”) into a string containing the equivalent ascii values and vice versa.
Do I have to cut the Hex string in pairs of 2 values and gue them together again or is there a convenient way to do that?

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-05-31T00:18:56+00:00Added an answer on May 31, 2026 at 12:18 am

    Based on binascii_unhexlify() function from Python:

    #include <cctype> // is*
    
    int to_int(int c) {
      if (not isxdigit(c)) return -1; // error: non-hexadecimal digit found
      if (isdigit(c)) return c - '0';
      if (isupper(c)) c = tolower(c);
      return c - 'a' + 10;
    }
    
    template<class InputIterator, class OutputIterator> int
    unhexlify(InputIterator first, InputIterator last, OutputIterator ascii) {
      while (first != last) {
        int top = to_int(*first++);
        int bot = to_int(*first++);
        if (top == -1 or bot == -1)
          return -1; // error
        *ascii++ = (top << 4) + bot;
      }
      return 0;
    }
    

    Example

    #include <iostream>
    
    int main() {
      char hex[] = "7B5a7D";
      size_t len = sizeof(hex) - 1; // strlen
      char ascii[len/2+1];
      ascii[len/2] = '\0';
    
      if (unhexlify(hex, hex+len, ascii) < 0) return 1; // error
      std::cout << hex << " -> " << ascii << std::endl;
    }
    

    Output

    7B5a7D -> {Z}
    

    An interesting quote from the comments in the source code:

    While I was reading dozens of programs that encode or decode the
    formats here (documentation? hihi:-) I have formulated Jansen’s
    Observation:

    Programs that encode binary data in ASCII are written in such a style
    that they are as unreadable as possible. Devices used include
    unnecessary global variables, burying important tables in unrelated
    sourcefiles, putting functions in include files, using
    seemingly-descriptive variable names for different purposes, calls to
    empty subroutines and a host of others.

    I have attempted to break with this tradition, but I guess that that
    does make the performance sub-optimal. Oh well, too bad…

    Jack Jansen, CWI, July 1995.

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

Sidebar

Related Questions

i want to build an object using this code $(input).bind(keydown,function(e){ var code = (e.keyCode
i want to know what wordpress build-in function you will use when sanitizing specific
I want to build a function, that one of it arguments is series {x[i],
I want to build a javascript function that maintains state. Here's a pattern that
For example, I want to build a DLL exporting a function called ShowDialog, which
I want to build pattern for preg_match that will match any string with length
I want to build an build a function that does return me an already
I want to build a js with a switch function. I want get the
I want to build a javascript function on my website to switch the text
I want to build my application with the function to restart itself. I found

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.