Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Binary conversion , Help please!!

Binary conversion , Help please!!

Scheduled Pinned Locked Moved C#
helptutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fara76
    wrote on last edited by
    #1

    I need to convert any integer value greate than 127 to binary value using this example: Represent the value in binary (e.g 137 => 1000 1001) 2) Break in up in groups of 7 bits from the lowest significant bit. (1 | 000 1001) 3) Take the lowest 7 bits and that gives you the lowest byte (0000 1001) 4) For the next group of 7 bits (in the example, this is 000 0001), set the MSB to 1 (which gives 1000 0001 in our example). Thus 137 becomes: 1000 0001 0000 1001 can anyone help me do this code using | & operators shif to left operators Thanks

    L P V 3 Replies Last reply
    0
    • F Fara76

      I need to convert any integer value greate than 127 to binary value using this example: Represent the value in binary (e.g 137 => 1000 1001) 2) Break in up in groups of 7 bits from the lowest significant bit. (1 | 000 1001) 3) Take the lowest 7 bits and that gives you the lowest byte (0000 1001) 4) For the next group of 7 bits (in the example, this is 000 0001), set the MSB to 1 (which gives 1000 0001 in our example). Thus 137 becomes: 1000 0001 0000 1001 can anyone help me do this code using | & operators shif to left operators Thanks

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I suggest you use unsigned integers for binary, octal, hexadecimal conversion. It is much easier, all 8/15/32/64 bits then behave in the same manner.

      Fara76 wrote:

      Break in up in groups of 7 bits

      That is definitely wrong, today's computers have 8 bits in a byte, and one or more bytes in their data path components (registers, ALUs, data bus, ...); there is absolutely no reason whatsoever to work in groups of 7 bits. :)

      Luc Pattyn


      try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


      1 Reply Last reply
      0
      • F Fara76

        I need to convert any integer value greate than 127 to binary value using this example: Represent the value in binary (e.g 137 => 1000 1001) 2) Break in up in groups of 7 bits from the lowest significant bit. (1 | 000 1001) 3) Take the lowest 7 bits and that gives you the lowest byte (0000 1001) 4) For the next group of 7 bits (in the example, this is 000 0001), set the MSB to 1 (which gives 1000 0001 in our example). Thus 137 becomes: 1000 0001 0000 1001 can anyone help me do this code using | & operators shif to left operators Thanks

        P Offline
        P Offline
        pmarfleet
        wrote on last edited by
        #3

        static void Main(string[] args) { int number = 0; try { if (args.Length == 0) { throw new Exception("Number not specified"); } if (!int.TryParse(args[0], out number)) { throw new Exception("Could not convert value to integer"); } const int power = 7; int tmpNumber = number; int i = 1; int maxValue = (int)Math.Pow((double)2, (double)power * i); while (number >= maxValue) { tmpNumber |= maxValue << 6; maxValue = (int)Math.Pow((double)2, (double)power * ++i); } Console.WriteLine(tmpNumber.ToString()); } catch (Exception ex) { Console.WriteLine("Exception occured"); while (ex != null) { Console.WriteLine(String.Format("Message: {0}", ex.Message)); Console.WriteLine(String.Format("Stack trace: {0}", ex.StackTrace)); ex = ex.InnerException; } } finally { Console.ReadLine(); } }

        F 1 Reply Last reply
        0
        • P pmarfleet

          static void Main(string[] args) { int number = 0; try { if (args.Length == 0) { throw new Exception("Number not specified"); } if (!int.TryParse(args[0], out number)) { throw new Exception("Could not convert value to integer"); } const int power = 7; int tmpNumber = number; int i = 1; int maxValue = (int)Math.Pow((double)2, (double)power * i); while (number >= maxValue) { tmpNumber |= maxValue << 6; maxValue = (int)Math.Pow((double)2, (double)power * ++i); } Console.WriteLine(tmpNumber.ToString()); } catch (Exception ex) { Console.WriteLine("Exception occured"); while (ex != null) { Console.WriteLine(String.Format("Message: {0}", ex.Message)); Console.WriteLine(String.Format("Stack trace: {0}", ex.StackTrace)); ex = ex.InnerException; } } finally { Console.ReadLine(); } }

          F Offline
          F Offline
          Fara76
          wrote on last edited by
          #4

          Thanks a lot, but would you explain the code please? i get 8329 for the value of 137? i don't understand why? and i also can you please explain how to change the final answer to bytes so i will have 100000001 00001001 in bytes ? Thanks

          1 Reply Last reply
          0
          • F Fara76

            I need to convert any integer value greate than 127 to binary value using this example: Represent the value in binary (e.g 137 => 1000 1001) 2) Break in up in groups of 7 bits from the lowest significant bit. (1 | 000 1001) 3) Take the lowest 7 bits and that gives you the lowest byte (0000 1001) 4) For the next group of 7 bits (in the example, this is 000 0001), set the MSB to 1 (which gives 1000 0001 in our example). Thus 137 becomes: 1000 0001 0000 1001 can anyone help me do this code using | & operators shif to left operators Thanks

            V Offline
            V Offline
            Vega02
            wrote on last edited by
            #5

            It looks like you're trying to implement a multi-precision integer. But outside of the MIDI specification I don't know where an MPI of this type would still be in use today, so it sounds almost like a homework assignment. What do you have so far? Maybe we could take a look at that and figure out what's going wrong in your code.

            F 1 Reply Last reply
            0
            • V Vega02

              It looks like you're trying to implement a multi-precision integer. But outside of the MIDI specification I don't know where an MPI of this type would still be in use today, so it sounds almost like a homework assignment. What do you have so far? Maybe we could take a look at that and figure out what's going wrong in your code.

              F Offline
              F Offline
              Fara76
              wrote on last edited by
              #6

              I finally fixed the code, it is working now! thanks anyway

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups