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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:44:39+00:00 2026-05-28T20:44:39+00:00

I have unsigned char[] that contains binary text. I need to convert this binary

  • 0

I have unsigned char[] that contains binary text. I need to convert this binary text into hexadecimal. I have managed to do just this and output the result to a file. However, for speed reasons, I would like to directly output this to a char[] so I can further manipulate it. The following is the code that writes out to the file:

for( i = 0; i < rsa.len; i++ )
fprintf( f, "%02X%s", buf[i],
             ( i + 1 ) % 16 == 0 ? "\r\n" : " " );

And buf is defined as follows:

unsigned char buf[512];

The output to the file looks like:

11 51 64 36 7A 9D 6C E8 F5 5C B2 29 2D 2F 1B 87
0A 20 23 3F B4 B0 41 5F A1 5F 54 6A C4 44 49 4B
58 C1 91 67 7D 7F 70 8D 20 9A 86 06 89 3A A8 2A
26 18 7A CE AB C0 7B 2A D7 A4 B2 5B C6 76 EB EA
90 F9 59 6C 78 4A 7C B2 A6 AE 46 3E E0 A7 A7 6A
F6 81 E3 70 78 B7 0D CC 8B D2 2C 23 42 EB 3B 90
88 15 AC 4C 84 FD 24 40 4F 08 C8 36 89 04 E4 5E
F1 AF CF FE 68 75 38 9C 75 0A 22 C2 44 49 35 A2

If it matters buf is the binary output of an 512 bit RSA encryption.

I was thinking of using sprintf() to print to a char[] in hexadecimal but I can’t seem to get it to work correctly.

  • 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-28T20:44:40+00:00Added an answer on May 28, 2026 at 8:44 pm

    The best-performant solution would be to write HEX yourself. This is not a difficult task if you use a lookup table:

    unsigned char buf[512];
    char res[2048];
    static const char* hex_lookup = "0123456789ABCDEF";
    char *p = res;
    for (int i = 0 ; i != 512 ; i++) {
        *p++ = hex_lookup[buf[i] >> 4];
        *p++ = hex_lookup[buf[i] & 0x0F];
        if ((i+1)%16) {
            *p++ = ' ';
        } else {
            *p++ = '\r';
            *p++ = '\n';
        }
    }
    *p = '\0';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a (void*) buffer that I need to convert to std::vector<unsigned char> before
I have an unsigned char array that I need in a std::string, but my
I have an array that is like this: unsigned char array[] = {'\xc0', '\x3f',
I have unsigned char* , want to convert it to std::string . Can you
Suppose we have a loop that runs for 100 times. Does using unsigned char
What I have in my project is the binary file that contains list of
I'm using VS 2008 and need to read text files that have UTF-8 Chinese
I have a std::vector<unsigned char> m_vData; m_vData.max_size() always returns -1. why would that happen?
Suppose that I have a unsigned char*, let's call it: some_data unsigned char* some_data;
I have a client that sends me data using this function: void CServerRetrieve::Send(char *buf,

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.