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:5069CC847069CC8420617261207061CC846E75692E2054616B7520776861CC846E6175202F206E61CC8420486FCC846E65204170616E7569203B206E61CC842053636F74742050656172736F6E206E6761CC842070696B697469612E20Your 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!TaNicole |
 |
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2008-09-03 : 23:55:20
|
-- a unicode stringdeclare @string nvarchar(100)set @string='hello world'-- convert it to hexdeclare @hex varbinary(100)set @hex=convert(varbinary(100), @string)-- convert it back to unicodeselect @string, @hex, convert(nvarchar(100), @hex) elsasoft.org |
 |
|
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 stringdeclare @string nvarchar(100)set @string='hello world'-- convert it to hexdeclare @hex varbinary(100)set @hex=convert(varbinary(100), @string)-- convert it back to unicodeselect @string, @hex, convert(nvarchar(100), @hex) elsasoft.org
|
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2008-09-04 : 23:09:46
|
try this:Select convert(nvarchar(100), 0x5069CC847069CC8420617261207061CC846E75692E2054616B7520776861CC846E6175202F206E61CC8420486FCC846E65204170616E7569203B206E61CC842053636F74742050656172736F6E206E6761CC842070696B697469612E20) elsasoft.org |
 |
|
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 |
 |
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
|
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. |
 |
|
Arnold Fribble
Yak-finder General
1961 Posts |
Posted - 2008-09-08 : 08:41:18
|
quote: Originally posted by jezeminedeclare @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! |
 |
|
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 jezeminedeclare @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!
|
 |
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
|
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=62406One day I'll get round to making a version of that function that takes varbinary input. |
 |
|
|