Jump to content
Sign in to follow this  
Wolvereness

_DecConvert()

Recommended Posts

I am proud to announce the first of 2-3 different converters that will convert numbers to/from any base system!

This one will convert a decimal number into any base system you would like. Note, when a character goes > Z it will place strange characters on the system. This function also converts numbers into binary! Further note, this function returns a STRING as so it wont be considered a floating number for base ten.

Usage: _DecConvert(Number to convert, Place values to have, Base system to use)

Return value: Number with place values (ones not used = 0) and proper base system.

Return value failure: 0 and @error = 1

Func _DecConvert($i_Number, $i_PlaceValue, $i_Base)
    If Abs(Int($i_Number)) = $i_Number And Abs(Int($i_PlaceValue)) = $i_PlaceValue And Abs(Int($i_Base)) = $i_Base And $i_Number And $i_PlaceValue And $i_Base Then
        Local $i_Count, $s_Values = '', $i_Temporary
        For $i_Count = 1 To $i_PlaceValue
            $i_Temporary = Int($i_Number / $i_Base ^ ($i_Count - 1) )
            If $i_Temporary Then
                $i_Temporary = Mod($i_Temporary, $i_Base)
                If $i_Temporary <> $i_Base Then
                    If $i_Temporary > 9 Then $i_Temporary = Chr(55 + $i_Temporary)
                    $s_Values = $i_Temporary & $s_Values
                    ContinueLoop
                EndIf
            EndIf
        $s_Values = '0' & $s_Values
        Next
        SetError(0)
        Return $s_Values
    Else
        SetError(1)
        Return 0
    EndIf
EndFunc
Edited by Wolvereness

Offering any help to anyone (to my capabilities of course)Want to say thanks? Click here! [quote name='Albert Einstein']Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.[/quote][quote name='Wolvereness' date='7:35PM Central, Jan 11, 2005']I'm NEVER wrong, I call it something else[/quote]

Share this post


Link to post
Share on other sites

Damn, I never came across that. I was working at school and I needed a Dec to Bin converter...

Oh well, I guess I'll just try to make the BaseToBase() converter...


Offering any help to anyone (to my capabilities of course)Want to say thanks? Click here! [quote name='Albert Einstein']Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.[/quote][quote name='Wolvereness' date='7:35PM Central, Jan 11, 2005']I'm NEVER wrong, I call it something else[/quote]

Share this post


Link to post
Share on other sites

I prefere keeping the two function separed since you can't actually convert from any base to an other any base, you always pass to the base 10.

Share this post


Link to post
Share on other sites

I mean was something like converting bin to hex. Something like:

_BaseToBase($i_Number, $i_OriginalBase, $i_NewBase)

Well, where you do not I will try. I'm still sad because my func had already been done...


Offering any help to anyone (to my capabilities of course)Want to say thanks? Click here! [quote name='Albert Einstein']Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.[/quote][quote name='Wolvereness' date='7:35PM Central, Jan 11, 2005']I'm NEVER wrong, I call it something else[/quote]

Share this post


Link to post
Share on other sites

Did you read what I said?

You can easily integrate the two functions, nothing to brag about, but they'll actually pass to the base ten.

E.g.

Base 2 to base 16

10011111, you have to calculate it in base 10 that means 159 raising to power all digits and after converting it to hex that means 9F with modulus operations.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...