DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Curious about the future of data-driven systems? Join our Data Engineering roundtable and learn how to build scalable data platforms.

Data Engineering: The industry has come a long way from organizing unstructured data to adopting today's modern data pipelines. See how.

Threat Detection: Learn core practices for managing security risks and vulnerabilities in your organization — don't regret those threats!

Managing API integrations: Assess your use case and needs — plus learn patterns for the design, build, and maintenance of your integrations.

Related

  • How to Convert Files to Thumbnail Images in Java
  • Understanding Floating-Point Precision Issues in Java
  • How To Convert Common Documents to PNG Image Arrays in Java
  • How To Create a Go CLI Tool That Converts Markdowns to PDF Files

Trending

  • Supporting Multiple Redis Databases With Infinispan Cache Aliases Enhancement
  • How to Create a Pokémon Breeding Gaming Calculator Using HTML, CSS, and JavaScript
  • Inside the World of Data Centers
  • How to Identify Bottlenecks and Increase Copy Activity Throughput in Azure Data Factory

Convert A Number From Any Base (between 2 And 36) To Base 10

By 
Snippets Manager user avatar
Snippets Manager
·
Sep. 14, 05 · Code Snippet
Likes (0)
Comment
Save
Tweet
Share
6.8K Views

Join the DZone community and get the full member experience.

Join For Free
@base can be anything from 2 to 36. This work is licensed under a Creative Commons Attribution 2.5 License.

CREATE FUNCTION dbo.f_convert_to_base10
  (@string VARCHAR(255), @base TINYINT)
RETURNS INT AS 
BEGIN

  -- Declarations
  DECLARE @return INT
  DECLARE @len INT
  DECLARE @finished BIT
  DECLARE @pos INT
  DECLARE @thischar CHAR(1)
  DECLARE @thisasc INT
  DECLARE @val INT

  -- Initialise
  SELECT @base     = CASE WHEN @base < 2 OR @base IS NULL THEN 2 WHEN @base > 36 THEN 36 ELSE @base END
  SELECT @return   = 0
  SELECT @finished = 0
  SELECT @string   = UPPER(@string)
  SELECT @len      = DATALENGTH(@string)

  -- Failsafe
  IF @len = 0
     SELECT @finished = 1

  -- Loop over all characters: capitalise first character and those after spaces, replace underscores with spaces
  SELECT @pos = 0

  WHILE @finished = 0
  BEGIN

    SELECT @pos = @pos + 1

    IF @pos > @len

       -- If we've run out of characters, we're done
       SELECT @finished = 1

    ELSE
    BEGIN

       -- Get the character (from right to left)
       SELECT @thischar = SUBSTRING(@string, (@len - (@pos - 1)), 1)

       -- Get the character's ASCII value
       SELECT @thisasc  = ASCII(@thischar)

       -- Convert to a numerical value
       SELECT @val = CASE
                       WHEN @thisasc BETWEEN 48 AND 57 -- '0' AND '9'
                         THEN @thisasc - 48
                       WHEN @thisasc BETWEEN 65 AND 90 -- 'A' (= decimal 10) AND 'Z'
                         THEN @thisasc - 65 + 10
                       ELSE 0 END

       -- Add this portion on
       SELECT @return = @return + (POWER(@base, (@pos - 1)) * @val)

    END

  END

  -- Done
  RETURN @return

END
GO


Example of usage:

SELECT 'FFFF' AS [hex], dbo.f_convert_to_base10('FFFF', 16) AS [decimal]
Convert (command)

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert Files to Thumbnail Images in Java
  • Understanding Floating-Point Precision Issues in Java
  • How To Convert Common Documents to PNG Image Arrays in Java
  • How To Create a Go CLI Tool That Converts Markdowns to PDF Files

Partner Resources


Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: