Gmail Calendar Documents Web Reader more »
Help | Sign in
Google Groups Home
Message from discussion Methoden zum Konvertieren zwischen Basis 10 und Basis 36
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Thomas Scheidegger [MVP]  
View profile   Translate to Translated (View Original)
 More options Jan 21 2006, 5:01 am
Newsgroups: microsoft.public.de.german.entwickler.dotnet.csharp
From: "Thomas Scheidegger [MVP]" <spam.netmas...@swissonline.ch>
Date: Fri, 20 Jan 2006 17:01:00 +0100
Subject: Re: Methoden zum Konvertieren zwischen Basis 10 und Basis 36
Hallo Marvin

    > Zahlen zwischen Basis 10 und Basis 36 (0-9, A-Z) hin und her konvertieren.

Eine ähnliche minimal-Lösung dazu hatte ich mal schon früher geschrieben,
geht auf diese Art wegen Assymetrie nicht für die negative Zahl = Int32.MinValue.
Ohne Gewähr:

// ==================================================
   // sample call:
   int n = Parse( "-XyZ", 36 );  // = -44027 decimal
   string r = ToString( n, 36 );

// --------
  public static int Parse( string text, int fromBase )
  {
   int l = text.Length - 1;
   int s = 0;
   if( text[0] == '-' )
    s++;

   int number = 0;
   for( int i = s; i <= l; i++ )
   {
    int ch = (int) text[ i ];
    if ((ch >= 48) && (ch <= 57))
     ch = ch - 48;
    else if ((ch >= 65) && (ch <= 90))
     ch = ch - 55;
    else if ((ch >= 97) && (ch <= 122))
     ch = ch - 87;
    else
     ch = Int32.MaxValue;
    if( ch >= fromBase )
     throw new ArgumentException("Invalid char");

    number = number * fromBase + ch;
   }
   if( s == 1 )
    number = -number;

   return number;
  }
// ----------
  public static string ToString( int number, int toBase )
  {
   char[] ach = new char[15];
   bool neg = false;
   if( number < 0 )
   {
    neg = true;
    number = Math.Abs( number );
   }
   int i = ach.Length;
   int rmd;
   do
   {
    number = Math.DivRem( number, toBase, out rmd );
    if( rmd >= 10 )
     ach[ --i ] = (char)(rmd + 55);
    else
     ach[ --i ] = (char)(rmd + 48);
   }
   while( number != 0 );

   if( neg )
    ach[ --i ] = '-';

   return new String( ach, i, ach.Length - i );
  }
// ==================================================

--
 Thomas Scheidegger - MVP .NET - 'NETMaster'
 http://www.cetus-links.org/oo_dotnet.html - http://dnetmaster.net/


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2010 Google