' *******************************************************
' Binary string to text | text to binary string functions
' Ported by vovchik from a PureBasic source
' *******************************************************

' ------------------
FUNCTION TO_TEXT(STRING bin_string$)
' ------------------
	' converts text to binary representation
	LOCAL char_code, x TYPE NUMBER
	LOCAL char_bin$, return_text$ TYPE STRING
	return_text$ = ""
	bin_string$ = REPLACE$(bin_string$, " ", "")
	IF MOD(LEN(bin_string$), 8) NE 0 THEN
		RETURN "ERROR: Length must be divisible by 8"
	END IF
	FOR x = 1 TO LEN(bin_string$) STEP 8
		char_bin$ = MID$(bin_string$, x, 8)
		char_code = 0
		IF MID$(char_bin$,1,1) = "1" THEN
			char_code = char_code + 128
		END IF
		IF MID$(char_bin$,2,1) = "1" THEN
			char_code = char_code + 64
		END IF
		IF MID$(char_bin$,3,1) = "1" THEN
			char_code = char_code + 32
		END IF
		IF MID$(char_bin$,4,1) = "1" THEN
			char_code = char_code + 16
		END IF
		IF MID$(char_bin$,5,1) = "1" THEN
			char_code = char_code + 8
		END IF
		IF MID$(char_bin$,6,1) = "1" THEN
			char_code = char_code + 4
		END IF
		IF MID$(char_bin$,7,1) = "1" THEN
			char_code = char_code + 2
		END IF
		IF MID$(char_bin$,8,1) = "1" THEN
			char_code = char_code + 1
		END IF
		return_text$ = CONCAT$(return_text$, CHR$(char_code))
	NEXT x
	RETURN return_text$
END FUNCTION

' ------------------
FUNCTION TO_BIN(STRING Text$)
' ------------------
	' converts binary representation to text
	LOCAL char_code, x TYPE NUMBER
	LOCAL char_text$, return_text$ TYPE STRING
	return_text$ = ""
	char_text$ = ""
	FOR x = 1 TO LEN(Text$)
		char_text$ = MID$(Text$, x, 1)
		char_code  = ASC(char_text$)
		IF char_code >= 128 THEN
			char_code = char_code - 128
			return_text$ = CONCAT$(return_text$, "1")
		ELSE
			return_text$ = CONCAT$(return_text$, "0")
		END IF
		IF char_code >= 64 THEN
			char_code = char_code - 64
			return_text$ = CONCAT$(return_text$, "1")
		ELSE
			return_text$ = CONCAT$(return_text$, "0")
		END IF
		IF char_code >= 32 THEN
			char_code = char_code - 32
			return_text$ = CONCAT$(return_text$, "1")
		ELSE
			return_text$ = CONCAT$(return_text$, "0")
		END IF
		IF char_code >= 16 THEN
			char_code = char_code - 16
			return_text$ = CONCAT$(return_text$, "1")
		ELSE
			return_text$ = CONCAT$(return_text$, "0")
		END IF
		IF char_code >= 8 THEN
			char_code = char_code - 8
			return_text$ = CONCAT$(return_text$, "1")
		ELSE
			return_text$ = CONCAT$(return_text$, "0")
		END IF
		IF char_code >= 4 THEN
			char_code = char_code - 4
			return_text$ = CONCAT$(return_text$, "1")
		ELSE
			return_text$ = CONCAT$(return_text$, "0")
		END IF
		IF char_code >= 2 THEN
			char_code = char_code - 2
			return_text$ = CONCAT$(return_text$, "1")
		ELSE
			return_text$ = CONCAT$(return_text$, "0")
		END IF
		IF char_code >= 1 THEN
			char_code = char_code - 1
			return_text$ = CONCAT$(return_text$, "1")
		ELSE
			return_text$ = CONCAT$(return_text$, "0")
		END IF
	NEXT x
	RETURN return_text$
END FUNCTION


' **************************
' MAIN
' **************************

' tests of both functions
PRINT "---"
org$ = "Vovchik says 'Hello' and 'Schönen Tag' to all BaConians!"
PRINT "ORG   : ", org$
PRINT "---"
a$ = TO_BIN(org$)
PRINT "TO_BIN: ", a$
PRINT "---"
b$ = TO_TEXT(a$)
PRINT "TO_TXT: ", b$
PRINT "---"
c$ = TO_BIN(b$)
PRINT "TO_BIN: ", c$
PRINT "---"
PRINT "It seems to work! :)"

' **************************
' END MAIN
' **************************