Display a string in binary form 
Author Message
 Display a string in binary form

Hi all,

How do I display a character string in binary and vice
versa?  

cString1 = "this is a test"

I would like to see 101010101010101111 101010101 etc.

Should I loop through the string and convert each char to
an int and then to binary? or there VFP functions that
will make the job easier?

Thanks in advance.



Sun, 28 Aug 2005 07:43:49 GMT  
 Display a string in binary form


Quote:
> Hi all,

> How do I display a character string in binary and vice
> versa?

> cString1 = "this is a test"

> I would like to see 101010101010101111 101010101 etc.

> Should I loop through the string and convert each char to
> an int and then to binary? or there VFP functions that
> will make the job easier?

> Thanks in advance.

Hi
cString1=''
cHex=StrConv('this is a test',15)
For i = 1 To Len(cHex) Step 2
 Asci= Evaluate('0x'+(Substr(hex,i,2)))
 cString1=cString1 + DecToBin(Asci)
Next
? cString1
Function DecToBin
Parameter InNum
OutStr=Space(0)
Do While InNum>0
 OutStr=Str(Mod(InNum,2),1,0)+OutStr
 InNum=Int(InNum/2)
Enddo
Return(OutStr)
Endfunc

-Anders
DecToBin has been posted by Daniel Tutuc, Hungary



Sun, 28 Aug 2005 09:54:41 GMT  
 Display a string in binary form

that would depend on word/byte length.. 8 bits? 8 bytes? 16 bits 16 bytes.

A = decimal 65
A = binary 1000001

Now; the problem is converting into a string.
If your only using 8 bits,
A would be 01000001

so you need to have a fixed length. so you know where each character
starts/stops.

Besides, in your examnple; you would need toc hange your space to:
0100000

anyway, here it is using an 8bit character

"this is a test" =
0111010001101000011010010111001100100000011010010111001100100000011000010010000001110100011001010111001101110100

the code for that is: (Are you going to ask for code to go from binary to
character now?)

* ConvertStringToBinary.prg
FUNCTION ConvertStringToBinary
  LPARAMETERS tcString
  LOCAL lcResult, x

  lcResult = ""

  FOR x = 1 TO LEN(tcString)
    lcResult = lcResult + DecToBin( ASC(SUBSTR(tcString,x,1)))
  ENDFOR
  RETURN lcResult
ENDFUNC

#DEFINE cNumAlpha '01'
FUNCTION DectoBin
  LPARAMETERS tnDecNum
  LOCAL lnQuotient, lnTemp, lcResInBase, lnCnt, lnDecNum

  lnDecNum = tnDecNum

  lcResInBase = ""      &&   string representation of base number
  lnCnt = 1
  DO WHILE lnDecNum >= LEN(cNumAlpha)^(lnCnt-1)
    lnCnt = lnCnt+1
  ENDDO
  lnCnt = lnCnt-1
  DO WHILE lnCnt > 0
    lnTemp = INT(lnDecNum / LEN(cNumAlpha)^(lnCnt-1)) + 1
    lcResInBase = lcResInBase + SUBSTR(cNumAlpha,lnTemp,1)
    lnDecNum = lnDecNum - ((lnTemp-1) * LEN(cNumAlpha)^(lnCnt-1))
    lnCnt = lnCnt-1
  ENDDO

  RETURN PADL(lcResInBase, 8, '0')
ENDFUNC

Quote:

>Hi all,

>How do I display a character string in binary and vice
>versa?  

>cString1 = "this is a test"

>I would like to see 101010101010101111 101010101 etc.

>Should I loop through the string and convert each char to
>an int and then to binary? or there VFP functions that
>will make the job easier?

>Thanks in advance.



Sun, 28 Aug 2005 10:23:08 GMT  
 Display a string in binary form


Quote:
> Hi all,

> How do I display a character string in binary and vice
> versa?

> cString1 = "this is a test"

> I would like to see 101010101010101111 101010101 etc.

> Should I loop through the string and convert each char to
> an int and then to binary? or there VFP functions that
> will make the job easier?

> Thanks in advance.

Here's an improved version
cText=(CHR(0)+"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ????")
cString1=''
For i = 1 To Len(cText)
 Asci= asc(SUBSTR(cText,i,1))
 ct=PADL(DecToBin(Asci),8,'0')

 cString1=cString1 + ct
NEXT
? cString1
? STRCONV(cText, 15)
Function DecToBin
Parameter InNum
OutStr=Space(0)
Do While InNum>0
 OutStr=Str(Mod(InNum,2),1,0)+OutStr
 InNum=Int(InNum/2)
Enddo
Return(OutStr)
Endfunc


Sun, 28 Aug 2005 10:48:03 GMT  
 Display a string in binary form
BITTEST(int,pos)  would do a better job

? Bytes("This is a test")
? Bytes("THIS IS A TEST")

* Bytes
FUNCTION Bytes
    PARAMETERS anyString
    IF EMPTY(anyString) OR TYPE("anyString") <> "C" THEN
        RETURN ""
    ENDIF
    LOCAL numChars, content, counter, bits
    numChars = LEN(anyString)
    content = ""
    FOR counter = 1 TO numChars
        bits = GetBits(ASC(SUBSTR(anyString,counter,1)))
        content = content + bits + IIF(counter < numChars,".","")
    ENDFOR
    RETURN content
ENDFUNC

* GetBits
FUNCTION GetBits(anyNumber As Integer)
    #DEFINE ZERO "0"
    #DEFINE ONE  "1"

    LOCAL bit, result
    result = ""
    FOR bit = 0 TO 7
        result = IIF(BITTEST(anyNumber,bit),ONE,ZERO) + result
    ENDFOR
    RETURN result
ENDFUNC


Quote:
> Hi all,

> How do I display a character string in binary and vice
> versa?

> cString1 = "this is a test"

> I would like to see 101010101010101111 101010101 etc.

> Should I loop through the string and convert each char to
> an int and then to binary? or there VFP functions that
> will make the job easier?

> Thanks in advance.



Tue, 30 Aug 2005 00:16:17 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. String length when calling binaries

2. Form display within a form

3. Form form Wizard does not display deleted records

4. Getting a form to NOT display the form

5. Question: Forms displayed inside other top-level forms

6. occurrence times of a string in other string

7. find a string in a string

8. SBT Accounting Software Programmers Mail String Forming

9. SBT Accounting Software Programmers Mail String Forming

10. SBT Programmer's Mail String Forming

11. Best way to exit a form, before display

12. Displaying vertical text in a form

 

 
Powered by phpBB® Forum Software