0

I need instance method(s) within the class am writing that convert from any base to base 10 & vice versa WIThOUT USING ANY LIBRARY ROUTINE.

```
//this is what i have so far
private int DecimalToBinary(int Decimal, int BaseNumber)
{
int Reminder;
do
{
Reminder = Decimal % BaseNumber;
}
while (Reminder > 0);
return.......; //need help on how to get/pick all the reminder together after above loop is excuted ?
```

*Edited
by Reverend Jim*: Fixed formatting

This Question has been **Answered**

0

This site should give you a very good idea how to do it.

Hint: Try to remember the outcome of your divisions and then print them out.

*Edited
by ddanbe*: n/a

0

I need instance method(s) within the class am writing that convert from any base to base 10 & vice versa WIThOUT USING ANY LIBRARY ROUTINE.

//this is what i have so far

private int DecimalToBinary(int Decimal, int BaseNumber)

{

int Reminder;

do

{

Reminder = Decimal % BaseNumber;

}

while (Reminder > 0);

return.......; //need help on how to get/pick all the reminder together after above loop is excuted ?

you're done after appending to the result. here are two ways to append the digits.

- collect the digits with StringBuilder, then convert to int with int.Parse.
`var result = new StringBuilder(); if (value != 0) { while (value != 0) { result.Append(GetDigit(value, radix)); value /= radix; } } else { result.Append("0"); } return int.Parse(result.ToString());`

- collect the digits directly into another int.
`int result = 0; if (value != 0) { while (value != 0) { result = 10 * result + (GetDigit(value, radix) - '0'); value /= radix; } } return result;`

#1 is easier to get right, but #2 is a lot faster.

i don't mind giving you code because you've got it mostly done and i think the real exercise is breaking the number down into digits anyway.

here's a fun little program that converts an int into a string of any base from 0 to 36 and even base64 like Convert.ToBase64String. it might give you some ideas. :)

```
using System;
using System.Text;
namespace Test
{
static class Program
{
static void Main()
{
int value = 5069456;
for (int i = 1; i < 38; i++)
{
try
{
Console.WriteLine("base {0:00}: {1}", i, value.ToBase(i));
}
catch (ArgumentOutOfRangeException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("unexpected exception: " + ex.Message);
}
}
Console.WriteLine("base 64: {0}", Convert.ToBase64String(BitConverter.GetBytes(value)));
}
/// <summary>
/// encodes an int value to the requested radix
/// </summary>
/// <remarks>
/// base 2 to 36 and base64 are supported
/// base64 follows RFC4648 rules
/// </remarks>
/// <param name="value">int value to encode</param>
/// <param name="radix">radix for the encoding</param>
/// <returns>base[radix] encoded string</returns>
static string ToBase(this int value, int radix)
{
if (radix != 64 && (radix < 2 || radix > 36))
{
throw new ArgumentOutOfRangeException(
"invalid radix [" + radix + "]." + Environment.NewLine +
"radix must be either 64 or between 2 and 36.");
}
// handle RFC4648 base64
if (radix == 64) return value.ToBase64();
// this algorithm doesn't like 0, just return tostring
if (value == 0) return value.ToString();
const string baseChars =
"0123456789" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var result = new StringBuilder();
int temp = value;
// strips and encodes each digit based on the radix
while (temp != 0)
{
result.Append(baseChars[temp % radix]);
temp /= radix;
}
// the encoded string is backward after the loop
return result.ToString().Reverse();
}
/// <summary>
/// encodes an int value using RFC4648 base64 encoding
/// </summary>
/// <param name="value">int value to encode</param>
/// <returns>base64 encoded string</returns>
static string ToBase64(this int value)
{
const string baseChars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz" +
"0123456789";
const char padding = '=';
byte[] bytes = BitConverter.GetBytes(value);
var retval = new StringBuilder();
for (int i = 0; i < bytes.Length; i += 3)
{
byte[] workingSet = new byte[3]; // 8-bit bytes to be encoded
byte[] sixBitGroup = new byte[4]; // 6-bit regrouping into 4 bytes
// gets the next 3 bytes from the bytestream with
workingSet[0] = bytes[i];
workingSet[1] = (i + 1 < bytes.Length) ? bytes[i + 1] : (byte)0;
workingSet[2] = (i + 2 < bytes.Length) ? bytes[i + 2] : (byte)0;
// regroup the 8-bit bytes into 6-bit bytes
sixBitGroup[0] = (byte)(workingSet[0] >> 2);
sixBitGroup[1] = (byte)(((workingSet[0] & 0x3) << 4) | (workingSet[1] >> 4));
sixBitGroup[2] = (byte)(((workingSet[1] & 0xf) << 2) | (workingSet[2] >> 6));
sixBitGroup[3] = (byte)(workingSet[2] & 0x3f);
// encode the 6-bit bytes using base64 chars
retval.Append(baseChars[sixBitGroup[0]]);
retval.Append(baseChars[sixBitGroup[1]]);
retval.Append((i + 1 < bytes.Length) ? baseChars[sixBitGroup[2]] : padding);
retval.Append((i + 1 < bytes.Length) ? baseChars[sixBitGroup[3]] : padding);
};
return retval.ToString();
}
/// <summary>
/// reverses the characters in a string
/// </summary>
/// <param name="s">original string</param>
/// <returns>reversed string</returns>
static string Reverse(this string s)
{
char[] chars = s.ToCharArray();
Array.Reverse(chars);
return new string(chars);
}
}
}
```

This question has already been answered. Start a new discussion instead.

Recommended Topics

I want to fetch invoice data from two tables

table a is in single row and table b data should be multiple rows

single data is fetching correctly but facing ...