Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 function to transform hexadecimal string to unicod

Author  Topic 

nicole_m
Starting Member

6 Posts

Posted - 2008-09-03 : 21:24:36
Hi there,
Does any of you have a function (in tsql ) that can transform a hexadecimal string into Unicode?

Here is an example of the string I have in one of my tables:

5069CC847069CC8420617261207061CC846E75692E2054616B7520776861CC846E6175202F206E61CC8420486FCC846E65204170616E7569203B206E61CC842053636F74742050656172736F6E206E6761CC842070696B697469612E20

Your help will be much appreciated!

Thanks,
Nicole

nicole_m
Starting Member

6 Posts

Posted - 2008-09-03 : 22:53:02
I have to add that I am working on a sql server 2000!
Ta
Nicole
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-09-03 : 23:55:20
-- a unicode string
declare @string nvarchar(100)
set @string='hello world'

-- convert it to hex
declare @hex varbinary(100)
set @hex=convert(varbinary(100), @string)

-- convert it back to unicode
select @string, @hex, convert(nvarchar(100), @hex)


elsasoft.org
Go to Top of Page

nicole_m
Starting Member

6 Posts

Posted - 2008-09-04 : 20:53:59
Hi jezemine,

thanks for that. unfortunatelly it doesnt solve my problem. If I put my hexadecimal string into your function returns same stuff!!

Select convert(nvarchar(100), '5069CC847069CC8420617261207061CC846E75692E2054616B7520776861CC846E6175202F206E61CC8420486FCC846E65204170616E7569203B206E61CC842053636F74742050656172736F6E206E6761CC842070696B697469612E20´')

quote:
Originally posted by jezemine

-- a unicode string
declare @string nvarchar(100)
set @string='hello world'

-- convert it to hex
declare @hex varbinary(100)
set @hex=convert(varbinary(100), @string)

-- convert it back to unicode
select @string, @hex, convert(nvarchar(100), @hex)


elsasoft.org

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-09-04 : 20:58:07
nicole_m, then we'd need to know the logic behind how that string is formed. Is it encrypted?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-09-04 : 23:09:46
try this:

Select convert(nvarchar(100), 0x5069CC847069CC8420617261207061CC846E75692E2054616B7520776861CC846E6175202F206E61CC8420486FCC846E65204170616E7569203B206E61CC842053636F74742050656172736F6E206E6761CC842070696B697469612E20)



elsasoft.org
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-09-04 : 23:41:52
or if you are actually storing the hex value as a string, you have to convert it to binary first:

declare @hexstring varchar(max);
set @hexstring = '0x5069CC847069CC8420617261207061CC846E75692E2054616B7520776861CC846E6175202F206E61CC8420486FCC846E65204170616E7569203B206E61CC842053636F74742050656172736F6E206E6761CC842070696B697469612E20';
declare @hex varbinary(max)
select @hex=cast('' as xml).value('xs:hexBinary( substring(sql:variable("@hexstring"), sql:column("t.pos")) )', 'varbinary(max)')
from (select case substring(@hexstring, 1, 2) when '0x' then 3 else 0 end) as t(pos)
select convert(nvarchar(100), @hex)

this wacky method courtesy of:
http://blogs.msdn.com/sqltips/archive/2008/07/02/converting-from-hex-string-to-varbinary-and-vice-versa.aspx


elsasoft.org
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-09-04 : 23:43:20
there's also this if you are on 2000 and don't like that xml nonsense:

http://sqlblog.com/blogs/peter_debetta/archive/2007/03/09/t-sql-convert-hex-string-to-varbinary.aspx


elsasoft.org
Go to Top of Page

nicole_m
Starting Member

6 Posts

Posted - 2008-09-08 : 00:33:27
I've got the string from an open query to an oracle dbase. This is the query:
SELECT * FROM OPENQUERY(MY_DATABASE, 'SELECT RAWTOHEX(TITLE) AS HEX_TITLE FROM PATRON_ADDRESS');

In SQL server I would like to revert that hex string to unicode char. With this I hope to solve the unicode char issues which came accross scrumbled from Oracle.


Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2008-09-08 : 08:41:18
quote:
Originally posted by jezemine
declare @hexstring varchar(max);
set @hexstring = '0x5069CC847069CC8420617261207061CC846E75692E2054616B7520776861CC846E6175202F206E61CC8420486FCC846E65204170616E7569203B206E61CC842053636F74742050656172736F6E206E6761CC842070696B697469612E20';
declare @hex varbinary(max)
select @hex=cast('' as xml).value('xs:hexBinary( substring(sql:variable("@hexstring"), sql:column("t.pos")) )', 'varbinary(max)')
from (select case substring(@hexstring, 1, 2) when '0x' then 3 else 0 end) as t(pos)
select convert(nvarchar(100), @hex)



Ah, I think you'll get more sense if you use varchar(200) than nvarchar(100) in that final convert: the string is UTF-8, not UTF-16.
(And despite what you get with the nvarchar(100) version, it looks like Maori, not Chinese to me!)

It says: "Pi¯pi¯ ara pa¯nui. Taku wha¯nau / na¯ Ho¯ne Apanui ; na¯ Scott Pearson nga¯ pikitia."

Sadly, all the combining macrons get turned into spacing ones when I post that!

But using "select convert(varchar(200), @hex)" isn't really the right answer: you really want to convert the UTF-8 octets into nvarchars.

In this case you could use "SELECT CAST(CAST(@hex AS xml) AS nvarchar(max))" as the last line, but this isn't generally a good idea since the five characters &<>"' will parsed by the XML parser!
Go to Top of Page

nicole_m
Starting Member

6 Posts

Posted - 2008-09-08 : 20:55:56
Hi guys,

Thanks for trying to help me out!
Unfortunately some stuff from Sql server 2005 is not available in 2000 :-(!

But you are right I don’t have Chinese char in my string! There are Maori indeed !!




quote:
Originally posted by Arnold Fribble

quote:
Originally posted by jezemine
declare @hexstring varchar(max);
set @hexstring = '0x5069CC847069CC8420617261207061CC846E75692E2054616B7520776861CC846E6175202F206E61CC8420486FCC846E65204170616E7569203B206E61CC842053636F74742050656172736F6E206E6761CC842070696B697469612E20';
declare @hex varbinary(max)
select @hex=cast('' as xml).value('xs:hexBinary( substring(sql:variable("@hexstring"), sql:column("t.pos")) )', 'varbinary(max)')
from (select case substring(@hexstring, 1, 2) when '0x' then 3 else 0 end) as t(pos)
select convert(nvarchar(100), @hex)



Ah, I think you'll get more sense if you use varchar(200) than nvarchar(100) in that final convert: the string is UTF-8, not UTF-16.
(And despite what you get with the nvarchar(100) version, it looks like Maori, not Chinese to me!)

It says: "Pi¯pi¯ ara pa¯nui. Taku wha¯nau / na¯ Ho¯ne Apanui ; na¯ Scott Pearson nga¯ pikitia."

Sadly, all the combining macrons get turned into spacing ones when I post that!

But using "select convert(varchar(200), @hex)" isn't really the right answer: you really want to convert the UTF-8 octets into nvarchars.

In this case you could use "SELECT CAST(CAST(@hex AS xml) AS nvarchar(max))" as the last line, but this isn't generally a good idea since the five characters &<>"' will parsed by the XML parser!


Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-09-08 : 22:54:25
if you don't have 2005 you can convert from string to hex using the function I already posted:

http://sqlblog.com/blogs/peter_debetta/archive/2007/03/09/t-sql-convert-hex-string-to-varbinary.aspx


elsasoft.org
Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2008-09-09 : 08:38:52
jezemine, that's not going to help with the UTF-8 to UTF-16 conversion.

Really, the UTF-8 octets ought to be converted directly from varbinary to nvarchar, but if you're willing to risk passing them through varchar then I did write a function to do the conversion:

http://www.sqlteam.com/Forums/topic.asp?TOPIC_ID=62406

One day I'll get round to making a version of that function that takes varbinary input.
Go to Top of Page
   

- Advertisement -