| xHarbour Reference Documentation > Operator Reference |
![]() |
![]() |
![]() |
Character operator (unary): retrieves a character from a string.
<cString>[<nPos>]
The character retrieves a single character at position <nPos> from the string <cString>. The value for <nPos> mus be in the range of 1 to Len(<cString>).
Note: if <nPos> is a negative value, the operator retrieves the character Abs(<nPos>) from the end of the the string.
| See also: | [ ] (array), SubStr() |
| Category: | Character operators , Operators , Special operators , xHarbour extensions |
| LIB: | xhb.lib |
| DLL: | xhbdll.dll |
// The example shows how to access and change characters in a string.
// Two functiosn are programmed which convert a numeric value to/from
// a string containing only "1" and "0".
PROCEDURE Main
LOCAL cStr
cStr := "xHarbour"
? cStr[5] // result: b
cStr[5] := "B"
? cStr // result: xHarBour
cStr[-1] := "B"
? cStr // result: xHarBouB
? cStr := Num2Bin( 52 )
// result: 00000000000000000000000000110100
? Bin2Num( cStr ) // result: 52.00
RETURN
FUNCTION Num2Bin( n )
LOCAL i, cBin := Padr("", 32, "0" )
FOR i:=1 TO 32
IF IsBit( n, i )
cBin[33-i] := "1"
ENDIF
NEXT
RETURN cBin
FUNCTION Bin2Num( cBin )
LOCAL n := 0
LOCAL i, imax := Len(cBin)
FOR i:=1 TO imax
IF cBin[imax+1-i]=="1"
n += 2ˆ(i-1)
ENDIF
NEXT
RETURN n
http://www.xHarbour.com