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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:21:08+00:00 2026-06-13T14:21:08+00:00

My goal is to write the more concise/effective function to convert a value to

  • 0

My goal is to write the more concise/effective function to convert a value to an hexadecimal string AS it is stored in memory (so the printed value will depends on the system endianness for example) :

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <string>

template<typename T> 
std::string hexOf(const T& x)
{
    return std::string(reinterpret_cast<const char*>(&x), sizeof(x));
}

int main()
{
   std::cout<<hexOf(9283)<<std::endl;
   return 0;
}

The current implementation does not work because the string contains the characters, but not the actual hex representation of the characters.

The final result I expect is hexOf(0xA0B70708) return the string 0807b7a0 on a little endian-system.

How to do that in a concise/effective way ?

  • 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-06-13T14:21:09+00:00Added an answer on June 13, 2026 at 2:21 pm

    Here’s the standard answer:

    template <typename T>
    std::string hexify(T const & x)
    {
        char const alphabet[] = "0123456789ABCDEF";
    
        std::string result(2 * sizeof x, 0);
        unsigned char const * const p = reinterpret_cast<unsigned char const *>(&x);
    
        for (std::size_t i = 0; i != sizeof x; ++i)
        {
            result[2 * i    ] = alphabet[p[i] / 16];
            result[2 * i + 1] = alphabet[p[i] % 16];
        }
    
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I write this jQuery more efficiently? The goal is to be able
My final goal is to write the program which can run on the Unix
Goal: Gain datatype date with year and month Problem: Need to help to convert
I've found a pattern in my Views like this: <% if (someCondition) { Response.Write(string.Format(Foo
I found myself confronted with an interview question where the goal was to write
Background / Goal More or less, I'm trying to create a nested group of
This is the general goal I am trying to achieve: My VB.NET program will
My goal is to write a program that handles an arbitrary number of tasks
Is it possible to write a filesystem for Windows in pure usermode, or more
I'm trying to learn to write a WordPress plugin by setting myself a goal

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.