Solved

CF Function to get number of bytes in a UTF-8 string?

Posted on 2004-03-31
6
369 Views
Last Modified: 2013-12-24
Hi

We are extending our application to support multi-lingual characters and we've upgraded to CFMX and Oracle 9i (built with UTF-8 character sets). So, each character in my CF form will become 3 times longer when I insert it into Oracle(for Japanese Kanji) and 2 times longer for European characters.

To ensure that I do not send in a string longer than the database field to Oracle, I'd have to verify and trim the string at the CF end. I found that at the Oracle end, I can check it using the LENGTHB and trim it using SUBSTRB functions, but I'm not able to find out what the equivalent CF function is ( because CF Len function always returns the number of characters and not the number of bytes)

Any help will be greatly appreciated.

Thanks
Greg
0
Comment
Question by:netuser1976
  • 3
  • 2
6 Comments
 
LVL 5

Expert Comment

by:Seth_Bienek
ID: 10728313

Hi Greg,

There's not really an easy way to do this without consuming beau-coups resources on your server.

Basically, you'd need to look at the input from the form, and check it to see if any of the characters exceed the Unicode code 0800 (3 byte), or 0080 (2 byte), or if they are all below 0080, meaning the is data is single-byte ASCII (which, by the way, is kind of a misnomer since it's actually ony 7 bits, not 8).

You could write a UDF to do this, but like I said, I think it would be resource-intensive since you have to check every character in your string to see if it is an extended character.

Maybe a regular expression is the way to go...?

Good luck,

Seth
0
 
LVL 2

Author Comment

by:netuser1976
ID: 10728348
Yeah I agree. It would be nice if macromedia gives us a function to do this, like a LenB in VB.
0
 
LVL 5

Expert Comment

by:Seth_Bienek
ID: 10728438
Meybe there is a native Java class you could extend?

Check your Java documentation and see if anything jumps out...
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 6

Accepted Solution

by:
PE_CF_DEV earned 50 total points
ID: 10732519
I beleive this will work:

 <cfset varlength=len(variable.getBytes('UTF-8'))>
This should return the proper length
0
 
LVL 2

Author Comment

by:netuser1976
ID: 10743188
THANKS PE_CF_DEV. That Worked..

Thanks Seth for your thoughts.
0
 
LVL 2

Author Comment

by:netuser1976
ID: 10743290
Any way to trim the string based on the byte count..

Trim(string, 1, 4000) where 4000 is the byte count...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now