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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:24:37+00:00 2026-06-01T12:24:37+00:00

I was recently working on a project where I needed to convert a regular

  • 0

I was recently working on a project where I needed to convert a regular string of numbers into ASCIII hexadecimal and store the hex in a string.

So I had something like

string random_string = "4000124273218347581"

and I wanted to convert it into a hexadecimal string in the form

string hex_string = "34303030313234323733323138333437353831"

This might seem like an oddly specific task but it’s one I encountered and, when I tried to find out how to perform it, I couldn’t find any answers online.

Anyway, I figured it out and created a class to make things tidier in my code.

In case anyone else needs to convert a regular string into a hexadecimal string I’ll be posting an answer in a moment which will contain my solution.

(I’m fairly new to stackoverflow so I hope that doing this is okay)

=========================================

Turns out I can’t answer my question myself within the first 8 hours of asking due to not having a high enough reputation.

So I’m sticking my answer here instead:

Okay, so here’s my solution:

I created a class called StringToHex in the namespace

public class StringToHex
{
    private string localstring;
    private char[] char_array;
    private StringBuilder outputstring = new StringBuilder();
    private int value;

    public StringToHex(string text)
    {
        localstring = text;
    }

    public string ToAscii()
    {
        /* Convert text into an array of characters */
        char_array = localstring.ToCharArray();
        foreach (char letter in char_array)
        {
            /* Get the integral value of the character */
            value = Convert.ToInt32(letter);

            /* Convert the decimal value to a hexadecimal value in string form */
            string hex = String.Format("{0:X}", value);

            /* Append hexadecimal version of the char to the string outputstring*/
            outputstring.Append(Convert.ToString(hex));
        }
    return outputstring.ToString();
    }
}

And to use it you need to do something of the form:

/* Convert string to hexadecimal */
StringToHex an_instance_of_stringtohex = new StringToHex(string_to_convert);
string converted_string = an_instance_of_stringtohex.ToAscii();

If it’s working properly, the converted string should be twice the length of the original string (due to hex using two bytes to represent each character).

Now, as someone’s already pointed out, you can find an article doing something similar here:

http://www.c-sharpcorner.com/UploadFile/Joshy_geo/HexConverter10282006021521AM/HexConverter.aspx

But I didn’t find it much help for my specific task and I’d like to think that my solution is more elegant 😉

  • 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-01T12:24:39+00:00Added an answer on June 1, 2026 at 12:24 pm

    This works as long as the character codes in the string is not greater than 255 (0xFF):

    string hex_string =
      String.Concat(random_string.Select(c => ((int)c).ToString("x2")));
    

    Note: This also works for character codes below 16 (0x10), e.g. it will produce the hex codes "0D0A" from the line break characters "\r\n", not "DA".

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

Sidebar

Related Questions

Recently in a project I am working on, I needed to store callbacks in
I am recently working in a project. There I need to convert language from
I have a project that I have recently started working on seriously but had
I'm working on a c++ project, and we recently needed to include a small
I was recently working on a project that involving connection to a POP3 Server.
I've recently begun working on a project regarding GUI building using some form of
I recently was working with a subversion project that checked out code not only
I recently began working on a large project that contains a huge number of
I have recently been working with someone on a project that is very ajax
I have recently started working on a very large C++ project that, after completing

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.