Adobe ColdFusion 8

Chr

View comments | RSS feed

Converts a numeric value to a UCS-2 character.

Returns

A character with the specified UCS-2 character code.

Category

String functions

Function syntax

Chr(number) 

See also

Asc

History

ColdFusion MX: Changed Unicode support: ColdFusion supports the Java UCS-2 representation of Unicode characters, up to a value of 65535. (Earlier releases supported 1-255.)

Parameters

Parameter

Description

number

A value (a number in the range 0 to 65535, inclusive)

Usage

The values 0 - 31 are standard, nonprintable codes. For example:

  • Chr(10) returns a linefeed character
  • Chr(13) returns a carriage return character
  • The two-character string Chr(13) & Chr(10) returns a Windows newline

Note: For a complete list of the Unicode characters and their codes, see www.unicode.org/charts/

Example

<!--- If the character string is not empty, output its Chr value. --->
<cfif IsDefined("form.charVals")>
        <cfoutput>#form.charVals# = #Chr(form.charVals)#</cfoutput>
</cfif>

<cfform action="#CGI.script_name#" method="POST">
    <p>Type an integer character code from 1 to 65535<br>
        to see its corresponding character.<br>
    <cfinput type="Text" 
        name="CharVals" 
        range="1,65535" 
        message="Enter an integer from 1 to 65535" 
        validate="integer" 
        required="Yes" 
        size="5" 
        maxlength="5"
        >
<p><input type="Submit" name=""> <input type="RESET">
</cfform>



Comments


Elliott Sprehn said on Jan 31, 2008 at 12:25 AM :
The line "The values 0 - 31 are standard, nonprintable codes." is misleading,
since CF doesn't actually provide the capacity to generate a NUL.

That is, chr(0) returns "" (empty string) not " ".

Which is odd, since Java has no issue with NUL in strings.

I guess this is carry over from the old C++ implementation of CF, but it's very
confusing if you need to actually create a NUL, for instance to write to a file
or for a protocol that requires it for a socket.

The easiest way I could figure how to get a NUL in a string is with:

createObject("java","java.lang.Character").init(javaCast("char",0)).toString()

It would be nice if CF did what the docs said when you passed in 0 though. :)
halL said on Feb 1, 2008 at 8:09 AM :
Elliott Sprehn is correct. The Chr(0) function returns the empty string, not NULL.
This was done by design, because ColdFusion does not support NULLs.

He describes a correct method to create a NULL if it is needed for uses such as File I/O or socket operations that require you to terminate the stream with a null character.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/8/htmldocs/functions_c-d_04.html