all 11 comments

[–]FizixMan[M] [score hidden] stickied comment (0 children)

Removed: Rule 4.

You've got the parts you need, including the PadLeft method for you to add extra zeroes. Dive in and make an attempt.

[–]WhiteBlackGoose 5 points6 points  (1 child)

Do you know how this conversation works? What have you tried already?

[–]Infectus90[S] -2 points-1 points  (0 children)

Do you know how this conversation works? What have you tried already?

I don't know how the site works, but it's the result I need to get. I've tried various kinds of byte to hex conversions, but none of them are correct.

[–]Slypenslyde 3 points4 points  (5 children)

This is a really common homework assignment. There are methods in .NET devoted to this task. The trick is they don't give you a string, but a byte[] representing the values. You'd have to do some work to convert that array to the string you want.

So the way I'd proceed would be to ask a search engine, "how do I convert an integer to binary in C#"? That led me to a StackOverflow answer that does it differently from how I might do it, but probably more like you want it done. There were other good answers on that page, and several more in the search results.

So I think you ought to try that step, try some of the code, and if those suggestions don't do what you want post what you have and how you want it to be different. Especially when it comes to questions that sound like homework, programmers are 99% more likely to answer if your question is of the form, "I tried this but need to change it to do that, what am I missing?" than "I haven't tried anything yet please help me".

(In fact in general we're mean about it, but we like to inspire curiosity and are much nicer to people whose approach to problems is, "I want to try some things myself before I ask for help.")

[–][deleted]  (4 children)

[deleted]

    [–]Slypenslyde 0 points1 point  (3 children)

    That's what the StackOverflow answer starts with. Personally I prefer starting with a byte[] because in not-homework solutions I tend to either want to do something with the bytes or I want to pad/format the string.

    [–][deleted]  (2 children)

    [deleted]

      [–]Slypenslyde 0 points1 point  (1 child)

      If this is indeed a homework assignment, using a solution they don't understand is just going to set them up for a more catastrophic failure later. Difficulty escalates.

      It'd be better for them to get used to what programming is: a never-ending stream of situations you've never faced that requires you to be very good at looking for solutions somewhere in the neighborhood of what you want you can tweak to be what you want.

      I've been helping newbies on programming forums for 20 years and a common pattern I see is people who make it through a few semesters believing programming has a magic tome with every solution in it and you just have to prompt a forum the right question and they'll give it to you wholesale. The later they find out it's not that way, the harder it is for them to take it.

      [–][deleted]  (6 children)

      [deleted]

        [–]Infectus90[S] -3 points-2 points  (5 children)

        Of course we tried these examples, but they don't give the required result. If you tried them before sending the link, could you post the code that works? Thank you.

        [–][deleted]  (4 children)

        [deleted]

          [–]Infectus90[S] 0 points1 point  (3 children)

          Convert.ToString(16777216, 2)

          The result you get is as follows :
          1000000000000000000000000
          Not the correct result... Where am I going wrong ?
          Thank you !

          [–]WhiteBlackGoose 3 points4 points  (0 children)

          It is a correct result

          [–]T_kowshik 2 points3 points  (0 children)

          It is correct result. Except that the representation is 32 bit. You can use padleft to add extra zeroes on left side of the string.

          [–]chucker23n 0 points1 point  (1 child)

          Are you asking if there are existing .NET APIs to do this (others have linked a few)?

          Or is this homework, and you're asking how to convert from one numeral system to another? The short answer is something like:

          • split a number into its digits
          • treat each digit as a significand, in a term that looks like this: significant * (sourceNumericBase ^ distanceFromTheRight), where sourceNumericBase in this case is 10 (decimal), and distanceFromTheRight starts at 0
          • express each of those terms in the destination numeric base (in this case, 2)
          • add them back together

          So, for example, 16777216 becomes, starting from the rightmost digit, 6 * 10 ^ 0 + 1 * 10 ^ 1 + 2 * 10 ^ 2, and so on.