Solved

asp / sql convert string to UCS2

Posted on 2011-02-20
4
777 Views
Last Modified: 2012-06-27
I need to convert strings to UCS2 hex.

can anyone provide me with either an asp function, or sql stored proc / function that will accomplish this?

for example to convert "abc" to "006100620063"
0
Comment
Question by:DanM711
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 34940314
Try this

select dbo.fnCharToUCSHex('abc')
CREATE FUNCTION fnCharToUCSHex(@input varchar(max)) 
RETURNS varchar(max)
AS
BEGIN
	
	DECLARE @position int, @asciicode int, @ResultVar varchar(max), @hex varbinary(2)
	
	SET @ResultVar = ''
	SET @position = 1

	WHILE @position <= DATALENGTH(@input)
	BEGIN
	   SET @asciicode = ASCII(SUBSTRING(@input, @position, 1))
	   SET @position = @position + 1
	   SET @hex = CONVERT(VARBINARY(2), @asciicode)
	   SET @ResultVar = @ResultVar + Right(master.dbo.fn_varbintohexstr(@hex), 4)
	END

	RETURN @ResultVar
END

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 34940343
This page from the Unicode web site http://www.unicode.org/faq/basic_q.html#14 says that UCS2 is the old version and UTF-16 is the current version.  And on another page http://en.wikipedia.org/wiki/UTF-16/UCS-2 :

The older UCS-2 (2-byte Universal Character Set) is a similar character encoding that was superseded by UTF-16 in version 2.0 of the Unicode standard in July 1996.

At the moment, I can't find a routine that does the conversion for you.
0
 
LVL 27

Expert Comment

by:BigRat
ID: 34942312
One needs to be a bit careful here. The Windows API MultiByteToWideChar will convert a byte string into a "Unicode" string. UTF-16 means that each character is encoded in 16 bits. There are however TWO formats for Unicode strings. The one format is "006100620063"  the other "610062006300". The first format is called Big Endian format the second Little Endian. The Little Endian format is the usual one found on most platforms, particularly Microsoft. Big Endian occurs often with IBM and is less prevelant. File which contain Unicode strings have a Byte Order Marker (BOM) at the beginning. Microsoft files, ie Little Endian, have hex FF FE at the beginning. Big Endian would have FE FF.

The format which the questioner is asking for is then Big Endian. is this correct?
0
 

Author Closing Comment

by:DanM711
ID: 34946423
Excellent, thank you so much, you really got me out of a hole there
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

707 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