• Hello everyone,

    We're happy to announce that our forum is back online! We needed to take some time off to discuss and decide on how to handle sensitive topics in our community.

    The team has posted an in-depth comment regarding a recent incident and our stance on politics and sexuality in our forum. We also added a dedicated paragraph about this in the forum guidelines. This is part of our commitment to maintain a welcoming and inclusive environment for all our users. Your understanding and continued support in building a creative and respectful community are highly appreciated.

    Thank you, the CX Dev-Team

Suggestion Conversion functions for binary and hex values

Phil7

Administrator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
821
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?
 
Definitely yes, very useful. And yes to your last question.
 
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
 
Cool, will try later
 
Works fine here. Can you add these yourself?
 
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