Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

A Technical Community for IT Professionals

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Highly Rated Users

Forum
No Posts Rated

Top 50
Given
Received

Forum Statistics

Users
Members:
1877
Members Online:
3
Guests Online:
92

Total Post History
Posts:
81447
Topics:
18714

7-Day Post History
New Posts:
0
New Topics:
0
Active Topics:
0

Our newest member
AlicjaHYBCIA

Other

FAQ
All times are UTC [ DST ]

Google Ads

number conversion

Mind Boggling Puzzles, to keep that grey matter in shape...
Forum rules
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
Please wait...

number conversion

Postby Thirster42 on Fri Apr 23, 2010 8:39 pm

Convert the following binary string into a decimal and a hexadecimal
01010100011111111100000



you're code must be dynamic. that means you should be able to pass a variable-length binary string and get a result
results should look like the following:
Dec: #, Hex: #

no built-in conversion functions (ie, no built-in function to convert a decimal to a hex, etc)

winner is the submitter who complies with the rules and has the shortest code (number of characters). if there is a tie, the winner goes to the submission with the most ratings.

The winner will be picked after 5/14/10, but feel free to keep posting answers.

*there is nothing to win, this is just for fun and bragging rights.
Ask three DBAs a question, and you’ll get four different answers.

-Brent Ozar
User avatar
Thirster42
Guru
Guru
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
 
Posts: 4646
Joined: Tue Nov 11, 2008 5:49 pm
Location: Jefferson City, Mo
Unrated

Re: number conversion

Postby Thirster42 on Fri Apr 23, 2010 8:41 pm

my code for the decimal portion.
Code is hidden, SHOW
Ask three DBAs a question, and you’ll get four different answers.

-Brent Ozar
User avatar
Thirster42
Guru
Guru
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
 
Posts: 4646
Joined: Tue Nov 11, 2008 5:49 pm
Location: Jefferson City, Mo
Unrated

Re: number conversion

Postby SQLDenis on Fri Apr 23, 2010 8:53 pm

stole your code to do this
Code is hidden, SHOW
User avatar
SQLDenis
LTD Admin
LTD Admin
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
 
Posts: 21784
Joined: Wed Oct 10, 2007 6:43 pm
Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Unrated

Re: number conversion

Postby Thirster42 on Fri Apr 23, 2010 9:00 pm

SQLDenis wrote:stole your code to do this
Code is hidden, SHOW


changed the rules on ya. :-D
Ask three DBAs a question, and you’ll get four different answers.

-Brent Ozar
User avatar
Thirster42
Guru
Guru
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
 
Posts: 4646
Joined: Tue Nov 11, 2008 5:49 pm
Location: Jefferson City, Mo
Unrated

Re: number conversion

Postby SQLDenis on Fri Apr 23, 2010 9:09 pm

No problem, here is even shorter code :-)
Code is hidden, SHOW
User avatar
SQLDenis
LTD Admin
LTD Admin
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
 
Posts: 21784
Joined: Wed Oct 10, 2007 6:43 pm
Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Unrated

Re: number conversion

Postby Thirster42 on Fri Apr 23, 2010 9:19 pm

added the variable to the appropriate places, the hex didn't return a value

Code is hidden, SHOW


and you're still using a built-in conversion anyway

  1. SELECT CONVERT(VARBINARY(30),SUM(VALUE))
Ask three DBAs a question, and you’ll get four different answers.

-Brent Ozar
User avatar
Thirster42
Guru
Guru
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
 
Posts: 4646
Joined: Tue Nov 11, 2008 5:49 pm
Location: Jefferson City, Mo
Unrated

Re: number conversion

Postby SQLDenis on Fri Apr 23, 2010 9:23 pm

Thirster42 wrote:added the variable to the appropriate places, the hex didn't return a value



and you're still using a built-in conversion anyway

  1. SELECT CONVERT(VARBINARY(30),SUM(VALUE))



okay, @ is enough, you don't need to name it :-)
  1. DECLARE @ VARCHAR(40)
  2. SET @= '01010100011111111100000'
  3.  
  4. SELECT @



BTW this only works on SQL Server 2008 and up

so this CONVERT(VARCHAR(MAX),CONVERT(VARBINARY(30),SUM(VALUE)),2)

won't work on 2005
User avatar
SQLDenis
LTD Admin
LTD Admin
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
LTD Gold - Rating: 3467LTD Gold - Rating: 3467LTD Gold - Rating: 3467
 
Posts: 21784
Joined: Wed Oct 10, 2007 6:43 pm
Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Unrated

Re: number conversion

Postby ThatRickGuy on Tue Apr 27, 2010 8:43 pm

Bin to Dec and Hex, written light on characters, current count: 196.

Code is hidden, SHOW


Turning off Option Strict gets it down to 184
Code is hidden, SHOW


-Rick
O_o \__o- \__o- \__o- \__o-
User avatar
ThatRickGuy
LTD Admin
LTD Admin
LTD Silver - Rating: 311LTD Silver - Rating: 311LTD Silver - Rating: 311LTD Silver - Rating: 311LTD Silver - Rating: 311
LTD Silver - Rating: 311
 
Posts: 1598
Joined: Thu Oct 11, 2007 3:21 pm
Location: Madison, WI

Re: number conversion

Postby AdamP on Tue Apr 27, 2010 10:27 pm

C#

Code is hidden, SHOW
AdamP
Newbie
Newbie
LTD Bronze - Rating: 6
 
Posts: 2
Joined: Fri Jun 05, 2009 7:20 pm

Re: number conversion

Postby AdamP on Wed Apr 28, 2010 2:04 pm

Rick I can't win with c# :) but if I turn Option Explicit Off and refactor yours a bit I can get it down to 227 characters (counting spaces) 159 (no spaces). Your last one had 270 counting spaces.

Code is hidden, SHOW
AdamP
Newbie
Newbie
LTD Bronze - Rating: 6
 
Posts: 2
Joined: Fri Jun 05, 2009 7:20 pm
Unrated

Re: number conversion

Postby ThatRickGuy on Wed Apr 28, 2010 2:52 pm

Not to let Adam beat me, ripping off his rip off of my rip off, current count: 150 characters.

Code is hidden, SHOW


-Rick
O_o \__o- \__o- \__o- \__o-
User avatar
ThatRickGuy
LTD Admin
LTD Admin
LTD Silver - Rating: 311LTD Silver - Rating: 311LTD Silver - Rating: 311LTD Silver - Rating: 311LTD Silver - Rating: 311
LTD Silver - Rating: 311
 
Posts: 1598
Joined: Thu Oct 11, 2007 3:21 pm
Location: Madison, WI
Unrated

Re: number conversion

Postby chrissie1 on Wed Apr 28, 2010 2:55 pm

We'll just add a couple million to that for the use of the .Net framework.
pink fuzzy slippers
User avatar
chrissie1
Senior Guru
Senior Guru
LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135
LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135LTD Gold - Rating: 2135
LTD Gold - Rating: 2135LTD Gold - Rating: 2135
 
Posts: 9475
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: number conversion

Postby ThatRickGuy on Mon May 03, 2010 4:55 pm

Adam and I got one of our coworkers (Kerry) to give it a try in C++. After a few iterations, he came up with this 149 character solution:

Code is hidden, SHOW


-Rick
O_o \__o- \__o- \__o- \__o-
User avatar
ThatRickGuy
LTD Admin
LTD Admin
LTD Silver - Rating: 311LTD Silver - Rating: 311LTD Silver - Rating: 311LTD Silver - Rating: 311LTD Silver - Rating: 311
LTD Silver - Rating: 311
 
Posts: 1598
Joined: Thu Oct 11, 2007 3:21 pm
Location: Madison, WI
Unrated

Re: number conversion

Postby ThatRickGuy on Mon May 03, 2010 5:37 pm

And because I am unwilling to allow VB.Net to be beaten in this challenge...

132

Code is hidden, SHOW


Edit: Kerry pointed out that by using an input box instead of a hard coded string, we could eliminate the example bit array string from the code. Good to shave off even more characters.
O_o \__o- \__o- \__o- \__o-
User avatar
ThatRickGuy
LTD Admin
LTD Admin
LTD Silver - Rating: 311LTD Silver - Rating: 311LTD Silver - Rating: 311LTD Silver - Rating: 311LTD Silver - Rating: 311
LTD Silver - Rating: 311
 
Posts: 1598
Joined: Thu Oct 11, 2007 3:21 pm
Location: Madison, WI
Unrated

Re: number conversion

Postby Thirster42 on Tue May 04, 2010 2:37 pm

ok, i'm going to set an end date for this, 5/14/10
Ask three DBAs a question, and you’ll get four different answers.

-Brent Ozar
User avatar
Thirster42
Guru
Guru
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
LTD Silver - Rating: 517LTD Silver - Rating: 517LTD Silver - Rating: 517
 
Posts: 4646
Joined: Tue Nov 11, 2008 5:49 pm
Location: Jefferson City, Mo
Unrated

Re: number conversion

Postby ThatRickGuy on Tue May 04, 2010 3:15 pm

Damnit, Kerry crushed me. He got it down to 127 in C++.

Code is hidden, SHOW
O_o \__o- \__o- \__o- \__o-
User avatar
ThatRickGuy
LTD Admin
LTD Admin
LTD Silver - Rating: 311LTD Silver - Rating: 311LTD Silver - Rating: 311LTD Silver - Rating: 311LTD Silver - Rating: 311
LTD Silver - Rating: 311
 
Posts: 1598
Joined: Thu Oct 11, 2007 3:21 pm
Location: Madison, WI
Unrated