Binary conversion , Help please!!
-
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
-
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
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] }
-
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
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(); } }
-
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(); } }
-
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
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.
-
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.