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

Latest Articles - DZone

article thumbnail
Convert A Number From Any Base (between 2 And 36) To Base 10
@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]
September 14, 2005
by Snippets Manager
· 7,433 Views
article thumbnail
Count Lines, Sentences, And Words Of A Text File
# count lines, sentences, and words of a text file # set all the counters to zero lines, blanklines, sentences, words = 0, 0, 0, 0 print '-' * 50 try: # use a text file you have, or google for this one ... filename = 'GettysburgAddress.txt' textf = open(filename, 'r') except IOError: print 'Cannot open file %s for reading' % filename import sys sys.exit(0) # reads one line at a time for line in textf: print line, # test lines += 1 if line.startswith('\n'): blanklines += 1 else: # assume that each sentence ends with . or ! or ? # so simply count these characters sentences += line.count('.') + line.count('!') + line.count('?') # create a list of words # use None to split at any whitespace regardless of length # so for instance double space counts as one space tempwords = line.split(None) print tempwords # test # word total count words += len(tempwords) textf.close() print '-' * 50 print "Lines : ", lines print "Blank lines: ", blanklines print "Sentences : ", sentences print "Words : ", words # optional console wait for keypress from msvcrt import getch getch()
July 24, 2005
by Snippets Manager
· 20,227 Views
article thumbnail
Swap Elements Of An Array In Ruby
class Array def swap!(a,b) self[a], self[b] = self[b], self[a] self end end You can now do stuff like.. [1,2,3,4].swap!(2,3) # = [1,2,4,3] etc.. Many thanks to Sam Stephenson and technoweenie for their suggestions.
May 13, 2005
by Snippets Manager
· 12,811 Views
article thumbnail
Converting Images To BufferedImages
Useful class for converting Image objects into BufferedImage objects. This is necessary if you are doing image manipulation since the majority of image manipulation code in Java processes BufferedImage objects. import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.awt.image.ImageObserver; /** * @author Anthony Eden */ public class BufferedImageBuilder { private static final int DEFAULT_IMAGE_TYPE = BufferedImage.TYPE_INT_RGB; public BufferedImage bufferImage(Image image) { return bufferImage(image, DEFAULT_IMAGE_TYPE); } public BufferedImage bufferImage(Image image, int type) { BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); Graphics2D g = bufferedImage.createGraphics(); g.drawImage(image, null, null); waitForImage(bufferedImage); return bufferedImage; } private void waitForImage(BufferedImage bufferedImage) { final ImageLoadStatus imageLoadStatus = new ImageLoadStatus(); bufferedImage.getHeight(new ImageObserver() { public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { if (infoflags == ALLBITS) { imageLoadStatus.heightDone = true; return true; } return false; } }); bufferedImage.getWidth(new ImageObserver() { public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { if (infoflags == ALLBITS) { imageLoadStatus.widthDone = true; return true; } return false; } }); while (!imageLoadStatus.widthDone && !imageLoadStatus.heightDone) { try { Thread.sleep(300); } catch (InterruptedException e) { } } } class ImageLoadStatus { public boolean widthDone = false; public boolean heightDone = false; } }
April 9, 2005
by Snippets Manager
· 7,818 Views · 7 Likes
  • Previous
  • ...
  • 1625
  • 1626
  • 1627
  • 1628
  • 1629
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook
×