• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Suggestion Conversion functions for binary and hex values

Phil7

Moderator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
886
I am still writing on the docs and I thought it would be nice to have some functions for debug printing of binary and hex values.
As it is now you can assign values in hex and binary format to integer variables but there is no way to show those values. At least I don't know of one.
My idea is to have functions similar to the inbuilt casting functions like String(int) but called something like BitString(Int) or BinString(Int) and HexString(Int), both returning a String.
What do you think about this?
And would you Add the prefixes $ and % to that String?
 
Here is my first try. Comments are welcome.
I made two versions of BinString because I was afraid performance is bad with string manipulations.
I tried to do the HexString without Modulo and with bit shifting, but it looked creepy and performance seems to be sufficient so far.
Code:
Strict 


Function Main:Int()   
    

    Print "~nComparison and Logic Operators:"
    Print BoolString(5 < 7)
    Print BoolString(5 > 7)
    Print BoolString(5 <= 7)
    Print BoolString(5 >= 7)
    Print BoolString(5 = 7)
    Print BoolString(True And True)
    Print BoolString(True Or False)
    Print BoolString(Not True)
    
    Print "~nBitwise Operators:"
    Print BinString(7)
    Print BinString(%111 Shl 3)
    Print BinString(256)
    Print BinString(256 Shr 3)

    Print "~nHex Values:"
     Print HexString($ABCDEF)
    Print HexString($ABCDEF Shl 4)
    Print BinString($ABCDEF)

    Return 0             
End


Function HexString:String(number:Int)
    
    Local hexArr:Int[65]
    Local chrArr:String[] = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]
    Local intSize:Int=0
    Local s:String = "$"
    
    Repeat
        hexArr[intSize] = number Mod 16
        number = number/16
        intSize +=1
    Until number = 0

    For Local i:Int = intSize-1 To 0 Step -1
        s += chrArr[hexArr[i]]
    Next
    
    Return s
End


Function BinString:String(Number:Int)
    Local s:String = ""
    Local s2:String = "%"
    Local bitCheck:Int = %1
    
    Repeat
        If Number & bitCheck
            s += "1"
        Else
            s += "0"
        Endif
        
        bitCheck = bitCheck Shl 1
    Until bitCheck = 0
    
    For Local i:Int=s.Length()-1 To 0 Step -1
        s2 += s[i..i+1]
    Next
    
    Return s2 '+ " " + (s2.Length()-1)
End


Function BinString2:String(Number:Int)
    
    Local binArr:Int[65]
    Local intSize:Int=0
    Local s:String = "%"
    Local bitCheck:Int = %1
    
    Repeat
        If Number & bitCheck
            binArr[intSize] = 1
        Endif
        
        bitCheck = bitCheck Shl 1
        intSize +=1
    Until bitCheck = 0
    
    For Local i:Int = intSize-1 To 0 Step -1
        If binArr[i]=1
            s += "1"
        Else
            s += "0"
        Endif
    Next
    
    Return s '+ " " + intSize
End


Function BoolString:String(boolValue:Bool)
    If boolValue
        Return "True"
    EndIf
    
    Return "False"
End
 
Yes, but not yet. I have to figure out how to deal with the sign bit and it has to be proven valuable with the docs first.
 
Here's a simple one that is double as fast, It's not the bit twiddling that does the trick though.

You can use it for binary too.

Code:
    Function HexString2:String(value:Int)
        Local m:String=""
        Repeat
            Local remainder:Int = value & 15
            value =  value Shr 4
            m = "0123456789abcdefghijklmnopqrstuvwxyz"[(remainder)..(remainder+1)] + m
        Until value = 0     
        Return m
    End

General binary / hexadecimal version
Code:
    Function AnyString:String(value:Int, base:Int)
        Local m:String=""
        Repeat
            Local remainder:Int = value Mod base
            value =  value / base
            m = "0123456789abcdefghijklmnopqrstuvwxyz"[(remainder)..(remainder+1)] + m
        Until value = 0      
        Return m
    End
 
Back
Top Bottom