Get the ASCII value for a number

Posted on 2004-11-22
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?

Question by:Mike_Stevens
    LVL 7

    Expert Comment

    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


    Expert Comment

    how about this ?

    MsgBox Asc("ascii number")

    kevin rea
    LVL 76

    Expert Comment

    by:David Lee
    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))

    Author Comment

    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:

    ....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?

    LVL 7

    Accepted Solution

    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
    LVL 76

    Assisted Solution

    by:David Lee
    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
            StringZero = String(intPadLength - intValLength, "0") & strValue
        End If
    End Function
    LVL 26

    Expert Comment

    you can do something like

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

    Expert Comment

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


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    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…

    731 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now