Re: Integer data type -> binary coded decimal
From: Steve Kass (skass_at_drew.edu)
Date: 03/20/04
- Next message: TomTom: "Can I output query result to text files in QueryAnalyzer?"
- Previous message: Steve Kass: "Re: View Partitioned"
- In reply to: Ali Cox: "Integer data type -> binary coded decimal"
- Next in thread: Ali Cox: "Re: Integer data type -> binary coded decimal"
- Reply: Ali Cox: "Re: Integer data type -> binary coded decimal"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 20 Mar 2004 12:00:05 -0500
Ali,
While it doesn't answer your specific question, here is a routine to
decode base64 data. It will decode the data into separate 3-byte pieces
that you can merge into the original longer string. The one detail I
didn't bother with is to determine whether the final piece is one, two,
or three bytes - you do this by looking at the number of = characters at
the end of the base64 string. Hope this helps, though it's a little
unfinished.
-- you'll need a permanent table of integers:
create table N8000 (
N int not null primary key
)
declare @i int
set @i = 0
while @i <= 8000 begin
insert into N8000 values (@i)
set @i = @i + 1
end
go
create table SampleData (
[base64] varchar(400)
)
insert into SampleData values ('bXlwYXNzd29yZA==') -- "mypassword" encoded
go
-- Here's the decoding routine
select
[base64],
N/4 as Chunk,
cast(sum(b*power(64,3-N%4)) as binary(3)) as Decoded
from (
select
[base64],
N,
case when c between 65 and 65+25 then c-65
when c between 97 and 97+25 then c-71
when c between 48 and 48+9 then c+4
when c = ascii('=') then 0
when c = ascii('+') then 62
when c = ascii('/') then 63 end as b
from (
select
[base64], N-1 as N, ascii(substring([base64],N,1)) as c
from N8000, SampleData
where N between 1 and len([base64])
) T
) T
group by [base64], N/4
order by [base64], N/4
go
-- Steve Kass
-- Drew University
-- Ref: 23DB840F-B9C7-4CCF-BD3C-49EDBEC8F0D0
Ali Cox wrote:
>I'm trying to create the binary coded decimal representation of a decimal integer, to be used/cast in a varchar concat with others of the same.
>Basically this is to decode MIME base64 encoding, and I have hit the proverbial brick wall with this part of it.
>
>EG. 34 (dec) is 100010 (bcd)
>
>I need to take the int 34, and change to the string '100010'.
>
>I hope there is a neat way to do this, perhaps mathematically?, as I don't really want to use a case statement. I can't see any datatype in SQL2K (Intel x86) that I can use.
>
>Thanks,
>Ali.
>
>
- Next message: TomTom: "Can I output query result to text files in QueryAnalyzer?"
- Previous message: Steve Kass: "Re: View Partitioned"
- In reply to: Ali Cox: "Integer data type -> binary coded decimal"
- Next in thread: Ali Cox: "Re: Integer data type -> binary coded decimal"
- Reply: Ali Cox: "Re: Integer data type -> binary coded decimal"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|