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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:38:41+00:00 2026-06-15T01:38:41+00:00

Ok so I have the following code that I looked up on the internet

  • 0

Ok so I have the following code that I looked up on the internet but for my project I am not allowed to use code and just throw it in if it works. I have to understand it and be able to give and explanation on the code and what it’s doing.

Convert.ToInt32("a").ToString("x");//ascii to hex
String.Join(String.Empty, 
      stringInput.Select(
         c => Convert.ToString(
            Convert.ToUInt32(c.ToString(), 16), 2).PadLeft(4, '0'))); 

Can you give me an explanation of these two lines please?

  • 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-15T01:38:42+00:00Added an answer on June 15, 2026 at 1:38 am

    This code looks complex, because the author tried to pack as much functionality into one line as possible. (Were I in a grouchier mood I’d call this “showing off” and would probably complain if a junior tried to check this in.) However, no single part of that line of code is complex; just break it apart piece by piece and examine each bit.

    For starters, that first line is just rubbish; ToInt32("a") throws a FormatException because you didn’t specify the base of 16, as: ToInt32("a", 16). If you fix it, though, it’s going to return 10.ToString("x") which is "a". So ignore that line, it does nothing useful. (We can imagine the author meant to use something like Encoding.UTF8.GetBytes() to return 0x61, but they did not.)

    The second line, you break down each bit and see what it does:

    String.Join(String.Empty, 
    

    This is pretty easy: it’s going to take an array of strings and join them together, using String.Empty as the separator (IOW, no separation.)

      stringInput.Select(
    

    Here’s we’re using LINQ to build up an IEnumerable of strings; it’s going to loop through the characters in the string, one at a time, and execute the lambda expression on each one.

         c => Convert.ToString(
            Convert.ToUInt32(c.ToString(), 16), 2).PadLeft(4, '0'))); 
    

    Here’s where it gets messy, because even for me matching up those parenthesis by eye is a pain, but if you expand the nested function calls into temporaries, you get:

    s = c.ToString();              // Convert the character to a string
    u = Convert.ToUInt32(s, 16);   // Convert a string of hex digits to a uint
    b = Convert.ToString(u, 2);    // Convert a uint to a string of binary digits.
    return b.PadLeft(4, '0')));    // Add 0s to make the string at least 4 digits.
    

    Each one of those “returned” values becomes the next element in the enumerable, which is returned by Select, which is then joined.

    The end result here is that you start with a string of hexadecimal digits from 0 – F which, when read as a number, has some particular numeric value, and you end up with a string of binary digits 0 and 1 which, when read as a number, has the same numeric value. IOW, you are “converting” a string of hex digits into an “equivalet” string of binary digits.

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

Sidebar

Related Questions

I have following code that I am compiling in a .NET 4.0 project namespace
I have following code that does not work due to a being a value
I have following code that does not work: I never get to goToFoodDetail .
I have following code snippet that i use to compile class at the run
I have the following code that works fine up to the last line in
I have some code (that is working), but I just want to make sure
I have looked all over the internet but could not get one final solution
I have the following code that has @item.ID from razor: <a href=# id=deleteitem(@item.ID)>Delete</a> When
I have the following code that gets the MAC address of an interface: static
I have the following code that opens a new popup window while disabling the

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.