• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 825
  • Last Modified:

Hex to Ascii Help

Hi.  I am trying to convert the contents of a field to ASCII.

The Field is of Image type and when viewed through the Gui is says Binary Data but when retreived via a select statement this is what is returned, HEX .

0x0015C5EF900800144F6AA35A080045000089AE464000400669710A000D050A0001B3A16761AF31A889D7FFB57FD25018C1E88AA300000105010100640401000000000000006100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005783000000000000E577

If I put this in a Hex to ASCII converter I get.

?????????Oj?Z??E????F@?@?iq?????????ga?1???????P????????????d?????????a???????????????????????????????????????????????????????????????????????W????????w

This the correct data (I cannot post an example with readable information)

How do I use a CLR or the like to convert this date in a SQL select statement?

Thanks

Matthew
0
Matthew_Lowe
Asked:
Matthew_Lowe
  • 7
  • 2
3 Solutions
 
Matthew_LoweAuthor Commented:
I have been looking at this and it kind of works but it only seems to convert the first 30 chars (I have increased all defs to MAX)

This would be great if it could convert the whole string (see above) .

I just cannot seem to work out how to do that. Any ideas?

Matthew
0
 
quincydudeCommented:
you see that bit in the code with
N'@ch varchar(30) OUTPUT    ?

could this be the cause?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Matthew_LoweAuthor Commented:
The code I am running is below. I had already changed it but there still seems to be a limit.

Thanks

Matthew  

declare @v varchar(8000), @sql nvarchar(4000), @ch varchar(8000)
select @v = '0x0015C5EF900800144F6AA35A080045000109AE3E4000400668F90A000D050A0001B3A17761AFF08CE0C0FF862F615018C1E8DE060000010501010064020100000000000000E1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011D82FBEFAA0000000000005783000000000000000F000000000000000000000000000000560000715753505F506F7274616C5F4F5554004A4D53436F7272656C6174696F6E49443D2749443A37353339613A31316438326662656661653A376239633A37663030303030313A31316438643862613333323A32383666653631316438643635353835656536302700'
select @sql = 'SELECT @ch = convert(varchar, ' +@v + ')'
EXEC sp_executesql @sql, N'@ch varchar(8000) OUTPUT', @ch OUTPUT
SELECT @ch,
upper(substring(master.sys.fn_varbintohexstr(convert(varbinary, @ch)),
3, 2*len(@ch))
0
 
Matthew_LoweAuthor Commented:
I have solved the reading all the values (see below). Now its sees to not like some special chars and stops. If I put a string in that only contains valid ASCII Chars its OK.

Is there a way to error check etc on this?

Mattew

declare @v varchar(8000), @sql nvarchar(4000), @ch varchar(8000)
select @v = '0x000D050A0001B3A17761AFF08CE0C0FF862F615018C1E8DE060000010501010064020100000000000000E1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011D82FBEFAA0000000000005783000000000000000F000000000000000000000000000000560000715753505F506F7274616C5F4F5554004A4D53436F7272656C6174696F6E49443D2749443A37353339613A31316438326662656661653A376239633A37663030303030313A31316438643862613333323A32383666653631316438643635353835656536302700'
select @sql = 'SELECT @ch = convert(varchar(MAX), ' +@v + ')'
EXEC sp_executesql @sql, N'@ch varchar(8000) OUTPUT', @ch OUTPUT
SELECT @ch,
upper(substring(master.sys.fn_varbintohexstr(convert(varbinary(MAX), @ch)),
3, 2*len(@ch)))
0
 
Matthew_LoweAuthor Commented:
00 is the issue. If I take out all the 00 from the HEX its OK.

I now need to find a way to Test for 00 and remove or replace with another char.

Thanks

Matthew
0
 
Matthew_LoweAuthor Commented:
Doing a Regex replace for 00 with 3f (?) works well.

Thanks for the help.

Matthew
0
 
Matthew_LoweAuthor Commented:
All fixed now
0
 
Matthew_LoweAuthor Commented:
All fixed now
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now