Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Get the ASCII value for a number

Posted on 2004-11-22
10
Medium Priority
?
223 Views
Last Modified: 2010-05-02
I am looking for a way to get the ASCII value for a passed number.  Meaning i need to know what the ASCII value of "375" would be.

I think that it would mean getting the value for each number and then adding them together.  Do anybody have any examples of how to do this with visual basic?


0
Comment
Question by:Mike_Stevens
8 Comments
 
LVL 6

Expert Comment

by:bkthompson2112
ID: 12648615
Hi Mike_Stevens,

"375" does not have an ASCII value.  
Each character of the string ('3', '7', and '5') has an ASCII value.
They are 51, 55, and 53, respectively.

Adding those number does not give the ASCII value of "375".

What are you trying to accomplish?

You can get the ASCII value of any character by using the Asc() function.
e.g.  debug.print asc("3")
prints 51

bkt
0
 

Expert Comment

by:kevinsrea
ID: 12648681
how about this ?

MsgBox Asc("ascii number")


kevin rea
0
 
LVL 76

Expert Comment

by:David Lee
ID: 12648691
I agree with what bkt said.  Still, if you want to add up ASCII values of each character position, then here's a way:

    strnumber = "375"
    For intPosition = 1 To Len(strnumber)
        intValue = intValue + Asc(Mid(strnumber, intPosition, 1))
    Next
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Mike_Stevens
ID: 12648783
I am trying to beat the sorting a recordset by a number value problem.  When i sort the recordset on the existing number value it is sorted as follows:

100
110
125
150
2
25
250
300
35
....and so on

I thought by getting the ascii value for the number is could sort the recordset based on that.  However, getting the ascii value for each number position on "375" does not work like i had hoped.  Any ideas on sorting a recordset on a number value that will sort correctly?


0
 
LVL 6

Accepted Solution

by:
bkthompson2112 earned 500 total points
ID: 12648881
Is the number saved in a text field?

If so, in your SQL statement, you should be able to convert the string number to
an actual number.  Then the sorting would work properly.

In SQL Server, I'd do something like this:

SELECT CAST(SortField AS int) as SortFieldNum
FROM tablename
ORDER BY SortFieldNum
0
 
LVL 76

Assisted Solution

by:David Lee
David Lee earned 500 total points
ID: 12648899
I don't know that this'll work with sorting a recordset, but when working with numbers it's always handy to make them all the same length by padding them with zeros to a certain length.  Here's a function that'll do that.  

Private Function StringZero(strValue As String, intPadLength As Integer) As String
    Dim intValLength As Integer
    strValue = Trim(strValue)
    intValLength = Len(strValue)
    If intValLength > intPadLength Then
        StringZero = strValue
    Else
        StringZero = String(intPadLength - intValLength, "0") & strValue
    End If
End Function
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 12654402
you can do something like


select number from yourtable order by val(number)
0
 
LVL 1

Expert Comment

by:dpalme
ID: 12698677
You need to have your values stores as numbers, integers, double, long, etc.  Then it will sort correctly.

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question