Results 1 to 4 of 4

Thread: Back with another SQL Question

  1. #1
    Senior Member skozzy's Avatar
    Join Date
    Jan 2008
    Location
    Brisbane
    Age
    55
    Posts
    525
    Thanks
    31
    Thanked 19 Times in 16 Posts
    Rep Power
    216
    Reputation
    114

    Default Back with another SQL Question

    I have a table in a game database with an INT field, that INT is made up of 6 sources equaling to 32 bits. The C Source shows they are all Unsigned Integers. But then final output is a Signed Int to be put back into SQL.

    At the moment I am using various web sites to convert decimal to binary for these 6 figures, then add the whole sting of 1's and 0's together then using a binary to decimal converter to make a final decimal number. While not hard its slow.
    I spent many hours reading examples and still don't really understand that they show, basically no real descriptions.

    So I have :
    (A) = 120 ... 8 bits
    (B) = 120 ... 8 bits
    (C) = 0 ...... 5 bits
    (D) = 14 .... 3 bits
    (E) = 20 .... 5 bits
    (F) = 4 ...... 3 bits

    The source INT value is -1810990984 (Signed)
    which breaks down to a binary string of:
    (F) (E) (C) (D) (B) (A)
    100 10100 000 01110 01111000 01111000
    4 20 0 14 120 120

    What I would like to do is Declare a,b,c,d,e,f as an INT (and change their values when I need), convert those INT's to a Binary string of correct length for (A) through (F), tie all the Strings to a single string, then convert that 32 bit binary string to a Signed INT.

    Sounds kind of out there to me, but I am still a noob with SQL. I have seen some examples but I can't yet grasp SQL enough to go solo on this. Just need help with the commands for conversions.
    Last edited by skozzy; 27-09-15 at 05:18 AM.



Look Here ->
  • #2
    Senior Member skozzy's Avatar
    Join Date
    Jan 2008
    Location
    Brisbane
    Age
    55
    Posts
    525
    Thanks
    31
    Thanked 19 Times in 16 Posts
    Rep Power
    216
    Reputation
    114

    Default

    Or.... If the way I explained it doesn't compute, then how would I display Decimal 120 as an 8 bit Binary string, and the number 20 as a 5 bit binary string, and the number 4 as a 3 bit binary string.
    Im still digging around to find out how to use SQL to convert a string of 32 0's and 1's to a Signed Decimal number.

  • #3
    LSemmens
    lsemmens's Avatar
    Join Date
    Dec 2011
    Location
    Rural South OZ
    Posts
    10,585
    Thanks
    11,867
    Thanked 7,061 Times in 3,338 Posts
    Rep Power
    3153
    Reputation
    132592

    Default

    Sounds more of a mathematical problem to me i.e. convert decimal to 8 bit binary and then reverse the process
    I'm out of my mind, but feel free to leave a message...

  • #4
    Senior Member skozzy's Avatar
    Join Date
    Jan 2008
    Location
    Brisbane
    Age
    55
    Posts
    525
    Thanks
    31
    Thanked 19 Times in 16 Posts
    Rep Power
    216
    Reputation
    114

    Default

    Yeah you are right, numbers to bin, add bin strings together, convert string to a number. I am getting there though ever so slowly.

    After a few days trawling the web I found someone that posted what I was after to convert a number to a binary string to the length I needed.

    --------------------------------------------------------------------------
    declare @intvalue int
    set @intvalue=120

    declare @vsresult varchar(8)
    declare @inti int
    select @inti = 8, @vsresult = ''
    while @inti>0
    begin
    select @vsresult=convert(char(1), @intvalue % 2)+@vsresult
    select @intvalue = convert(int, (@intvalue / 2)), @inti=@inti-1
    end
    select @vsresult
    --------------------------------------------------------------------------

    Now my last problem is converting a 32 bit binary string to a 'Signed' Decimal number:
    01010010000100010111001101110011 = 1376875379 as a Signed Number

    This website is what I am using for now :

    But really hoping there is a SQL function or code to do this.

  • Bookmarks

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •