View comments | RSS feed
Contents > CFML Reference > ColdFusion Functions > ToString PreviousNext

ToString

Converts a value to a string.

A string.

Conversion functions, Other functions, String functions

ToString(any_value[, encoding])

ToBase64, ToBinary

ColdFusion MX:

Parameter

Description

any_value

Value to convert to a string

encoding

The character encoding (character set) of the string. The following list includes commonly used values:

  • utf-8
  • iso-8859-1
  • windows-1252
  • us-ascii
  • shift_jis
  • iso-2022-jp
  • euc-jp
  • euc-kr
  • big5
  • euc-cn
  • utf-16

For more information on character encoding, see:
www.w3.org/International/O-charset.html.

Default: the encoding of the page on which the function is called. See cfcontent.

This function can convert simple values and binary values that do not contain Byte zero. If this function cannot convert a value, it throws an exception. This function can convert an XML document object to a string representation.

Note: You can use this function to reverse Base64 encoding of a string. Convert the Base64 encoded object to a binary object, then use this function to convert the binary object to a string.

<h3>ToString Example</h3>
<!---- Initialize data. ------>
<cfset charData = "">
<!----- Create string of ASCII characters (32-255) and concatenate them. ---->
<cfloop index = "data" from = "32" to = "255">
   <cfset ch = chr(data)>
   <cfset charData = charData & ch>
</cfloop>
<p>The following string is the concatenation of characters (32 to 255) 
from the ASCII table.<br> <cfoutput>#charData#</cfoutput></p> <!------ Create a Base64 representation of this string. ----> <cfset data64 = toBase64(#charData#)> <p> The following string is the Base64 representation of the string.<br> <cfoutput>#data64#</cfoutput></p> <!---- Create a binary representation of Base64 data. ---> <cfset dataBinary = toBinary(data64)> <!---- Create the string representation of the binary data. -----> <cfset dataString = ToString(dataBinary)> <p>The following is the string representation of the binary data.<br> <cfoutput>#dataString#</cfoutput></p>

Contents > CFML Reference > ColdFusion Functions > ToString PreviousNext

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


halL said on Sep 5, 2003 at 2:25 PM :
The current version of the documentation is missing the following:

You can use the Encoding parameter only when you convert binary data to string data.
If you specify an encoding when you convert any other type of data to a string, ToString generates an error.

Explanation:
When converting binary data to a string, the internal conversion routine must know the original textual encoding of the data so that it can properly transform it to a ColdFusion (actually Java) internal string representation, which is encoded in UTF-16.
For example, if binary data represents ISO 8859-1 (8 bit) characters, the conversion routine must convert and expand each 8-bit byte into a 16-bit UTF-16 character,
whereas if the binary data represents UTF-16, no conversion is necessary (other than removing any byte order mark).
When converting non-binary data, the data format is already known, so specifying the original encoding is not appropriate
Iceborer said on Jun 17, 2004 at 8:17 AM :
When converting an XML document object using toString, I cannot specify encoding (apparently an XML Doc is not considered a binary object). Because of this I run into the following issue:

<cfset xml_string = toString (XMLDoc)>
<cffile action="write" file="myfile.xml" output="#xml_string#">

results in a file which has an XML header specifying UTF-8 enconding
(<?xml version="1.0" encoding="UTF-8"?>)
in which all of the data is written in UTF-16

00000000h: FF FE 3C 00 3F 00 78 00 6D 00 6C 00 20 00 76 00 ; ÿþ<.?.x.m.l. .v.

If I can't specify an encoding when I toString an XML Doc, how do I fix this?
clinecorp said on Jun 25, 2004 at 5:51 PM :
when an XML object is dumped using toString(), all &apos; entity values are replaced with single quotes ('). This is undesirable behaviour as it creates a technically invalid XML document and confuses those trying to keep XML specific chars as entities within the database.
TacoBell said on Dec 16, 2004 at 2:31 PM :
When I call toStirng() on a valid XML document that contains
<link href="" rel="" />

it converts it to <link href="" rel="">
whats up with that? I did not tell it I wanted HTML..
I'm still trying to find a way to overcome this huge problem.

 

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

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/funca110.htm