Question

0x323031332D31312D30372031303A34393A32393B2032C2F7BFF820B9D9C4DAB5E520B8F0B5E220B7CEB5E52E2E2E0D0A323031332D31312D30372031303A34393A32393B20B8AEC6F7C6AE20BCADBAF1BDBAB8A620BBFDBCBAC7D5B4CFB4D90D0A323031332D31312D30372031303A34393A32393B2042494E445F49503A20302E302E302E302C20504F52543A203730363720BCADBAF1BDBAB0A120BDC3C0DBB5C7BEFABDC0B4CFB4D90D0A323031332D31312D30372031343A32353A34353B2032C2F7BFF820B9D9C4DAB5E520B8F0B5E220B7CEB5E52E2E2E0D0A323031332D31312D30372031343A32353A34353B20B8AEC6F7C6AE20BCADBAF1BDBAB8A620BBFDBCBAC7D5B4CFB4D90D0A323031332D31312D30372031343A32353A34353B2042494E445F49503A20302E302E302E302C20504F52543A203730363720BCADBAF1BDBAB0A120BDC3C0DBB5C7

Hello. Newbie asking for help.

What I have to do is extract this binary data from a mssql 2005 DB and write it in to a file.

I found out that I can convert the data using CONVERT but I don't know how exactly I should type it in the query. A lot of answer is about 2008 and those answers about 2005 I don't get the logic. for example

declare @b varbinary(max)
set @b = 0x5468697320697320612074657374
select cast(@b as varchar(max)) /*Returns "This is a test"*/ 

I don't know what should I type for my code after set@b =

BTW I'm using python to make this program. So I'm using pyodbc to connect to the DB and using .write('data') to write it into a file.

p.s.: The data's characters was over 60k so I had to cut most of the data to post this.

Was it helpful?

Solution

data = 0x323031332D31312D30372031303A34393A32393B2032C2F7BFF820B9D9C4DAB5E520B8F0B5E220B7CEB5E52E2E2E0D0A323031332D31312D30372031303A34393A32393B20B8AEC6F7C6AE20BCADBAF1BDBAB8A620BBFDBCBAC7D5B4CFB4D90D0A323031332D31312D30372031303A34393A32393B2042494E445F49503A20302E302E302E302C20504F52543A203730363720BCADBAF1BDBAB0A120BDC3C0DBB5C7BEFABDC0B4CFB4D90D0A323031332D31312D30372031343A32353A34353B2032C2F7BFF820B9D9C4DAB5E520B8F0B5E220B7CEB5E52E2E2E0D0A323031332D31312D30372031343A32353A34353B20B8AEC6F7C6AE20BCADBAF1BDBAB8A620BBFDBCBAC7D5B4CFB4D90D0A323031332D31312D30372031343A32353A34353B2042494E445F49503A20302E302E302E302C20504F52543A203730363720BCADBAF1BDBAB0A120BDC3C0DBB5C7

Convert the number to hexadecimal representation, then use binascii.hexlify to get the original string (or bytes):

>>> h = format(data, 'x')
'323031332d31312d30372031303a34393a32393b2032c2f7bff820b9d9c4dab5e520b...'
>>> import binascii
>>> print(binascii.unhexlify(h))
2013-11-07 10:49:29; 2차원 바코드 모듈 로드...
2013-11-07 10:49:29; 리포트 서비스를 생성합니다
2013-11-07 10:49:29; BIND_IP: 0.0.0.0, PORT: 7067 서비스가 시작되었습니다
2013-11-07 14:25:45; 2차원 바코드 모듈 로드...
2013-11-07 14:25:45; 리포트 서비스를 생성합니다
2013-11-07 14:25:45; BIND_IP: 0.0.0.0, PORT: 7067 서비스가 시작되

NOTE the bytes are encoded in cp949 (or euc-kr) encoding.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top