CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Oct 2001
    Posts
    26

    Converting ASCII (byte) to Decimal

    I have this string HEX values 02 CB 02 and its integer value is 715 or 727. Is there a function or how would I go about converting it to a decimal ?

    Thank you


  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Converting ASCII (byte) to Decimal

    put &H before the hex value.
    Ie:
    MsgBox &H2CB02
    will display the dec value

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  3. #3
    Join Date
    Oct 2001
    Posts
    26

    Re: Converting ASCII (byte) to Decimal

    That is incorrect, perhaps I am not explaining myself.

    02 CB 02 are a hex output from a BINARY long variable. How do I convert it to Decimal ?

    Thank you


  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Converting ASCII (byte) to Decimal

    I do not have answer for sure, as I did not played a lot with math...
    may dec value be 812?
    Print &H2 * &HCB * &H2
    (which is= 2* 203*2)

    I also tried:
    (&H2) ^ 0 + (&HCB) ^ 1 + (&H2) ^ 2
    which gave only 208...

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Oct 2001
    Posts
    26

    Re: Converting ASCII (byte) to Decimal

    Ok, heres what it is, if A=715, then visual basic stores that in memory, it stores it as 02 CB 02 ....

    Now how do I get it to tell me the value only when I have the "memory" dump ?

    Thx


  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: 715

    odds, you know?
    715 => hex = 02 CB
    last 02 should be something else (maybe representation of sign?)

    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    Oct 2001
    Posts
    26

    Re: 715

    Well it is as 02 but the value of A might is 727 or 715,

    Now what I need I guess is a method of copying these values directly in memory or something. ???




  8. #8
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Sorry, jasiu...

    ...but I do not know what I am talking about. To me, ascii value of "A" is 65 and of "a" is 95. I do not even understand how it could be 715 or about...
    However, if this may be of any help, there's an api called Copymemory
    from api-guide:
    "The CopyMemory function copies a block of memory from one location to another.
    Requires Windows NT 3.1 or later; Requires Windows 95 or later
    example:

    private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst as Any, pSrc as Any, byval ByteLen as Long)
    private Declare Function GetTickCount Lib "kernel32" () as Long
    private Sub Form_Load()
    'KPD-Team 1999
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    Dim sSave as string, Cnt as Long, T as Long, Pos as Long, Length as Long
    Const mStr = "Hello "
    Length = len(mStr)
    sSave = Space(5000 * Length) 'make buffer for justified comparison
    'get the current tickcount
    T = GetTickCount
    Pos = 1
    sSave = Space(5000 * Length)
    for Cnt = 1 to 5000
    mid(sSave, Pos, Length) = mStr
    Pos = Pos + Length
    next Cnt
    'Show the results
    MsgBox "It took Visual basic" + Str$(GetTickCount - T) + " msecs. to add 5000 times a string to itself."
    'get the current tickcount
    T = GetTickCount
    Pos = 0
    sSave = Space(5000 * Length)
    for Cnt = 1 to 5000
    CopyMemory byval StrPtr(sSave) + Pos, byval StrPtr(mStr), LenB(mStr)
    Pos = Pos + LenB(mStr)
    next Cnt
    'Show the results
    MsgBox "It took CopyMemory" + Str$(GetTickCount - T) + " msecs. to add 5000 times a string to itself."
    End Sub





    Hope this may help


    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  9. #9
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Converting ASCII (byte) to Decimal

    Private Const MaxPower As Byte = 31 'maximum power of 2 we can do because 2^31 is the maximum value of a long

    Private Sub cmdBTD_Click()
    Dim i As Integer
    Dim CurrentDec As Double

    'the formula to use here is: for each binary digit, we
    'sum to the actual decimal number: digit_value * 2 ^ character_position
    ' so a binary number like this: 1101
    ' will result in:
    ' 1 * 2 ^ 3 + 1 * 2 ^ 2 + 0 * 2 ^ 1 + 1 * 2 ^ 0
    ' what means: 8 + 4 + 0 + 1 = 13

    For i = 1 To Len(txtBIN)
    CurrentDec = CurrentDec + CLng(Mid(txtBIN, i, 1)) * (2 ^ (Len(txtBIN) - i))
    Next
    txtDEC.Text = CurrentDec
    End Sub






    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  10. #10
    Join Date
    Oct 2001
    Posts
    2

    Re: Converting ASCII (byte) to Decimal

    write out the hex number, placing each digit under the appropriate decimal value for that position. Multiply the decimal value by the base 16 digit and add the values. (Convert A through F to their decimal equivalent before multiplying).
    The decimal equivalent of 2C is 44
    c = 12...so
    2 * 16^1 + 12 * 16^0
    32+ 12 = 44.

    Hence decimal equvivalent of 2CB02 is definitely not 715 or 727. The best way to approach is to either convert this value to Long or Double using CLng or CDbl


  11. #11
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Converting ASCII (byte) to Decimal

    The decimal value for a hex 02CB02 = 183042 not the 712 or 715 you expect. HEx values are base 16 so it computes something like this
    Digit 0 = 1,048,576 * 0 = 0
    Digit 2 = 65,526 * 2 = 131072
    Digit C = 4,096 * 12 = 49152
    Digit B = 256 * 11 = 2816
    Digit 0 = 16 * 0 = 0
    Digit 2 = 0 + 2 = 2

    John G

  12. #12
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Converting ASCII (byte) to Decimal

    Slight error
    Digit 2 should be 65536 not 65526

    John G

  13. #13
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Converting ASCII (byte) to Decimal


    option Explicit


    public Function ConvertIt(D1, D2, D3)
    Dim x as Long, x1 as Long
    Dim y as Long, y1 as Long
    Dim z as Long, z1 as Long

    x = Int(D1 / 65536)
    x1 = D1 Mod 65536
    y = Int(D2 / 256)
    y1 = D2 Mod 256
    z = Int(D3 / 16)
    z1 = D3 Mod 16
    ConvertIt = (x * 1048576) + (x1 * 65536) + (y * 4096) + (y1 * 256) + (z * 16) + z1
    End Function

    private Sub Command1_Click()
    Dim lRet as Long
    Dim a as Byte
    Dim b as Byte
    Dim c as Byte
    a = &H2
    b = &HCB
    c = &H2
    lRet = ConvertIt(a, b, c)
    MsgBox lRet
    End Sub




    John G

  14. #14
    Join Date
    Oct 2001
    Location
    Hyderabad,India
    Posts
    8

    Re: Converting ASCII (byte) to Decimal

    Hi,
    I did not get the exact problem. But try this function you might get the decimal value of Hex.


    private Function HexToDec(strHex as string) as Long
    Dim lngHexFromAToF as Long
    Dim strEachCharFromstrHex as string

    Do While (strHex <> "")
    strEachCharFromstrHex = mid(strHex, 1, 1)
    Select Case UCase(strEachCharFromstrHex)
    Case "A"
    lngHexFromAToF = 10
    Case "B"
    lngHexFromAToF = 11
    Case "C"
    lngHexFromAToF = 12
    Case "D"
    lngHexFromAToF = 13
    Case "E"
    lngHexFromAToF = 14
    Case "F"
    lngHexFromAToF = 15
    End Select
    If lngHexFromAToF = 0 then
    If IsNumeric(strEachCharFromstrHex) then
    HexToDec = HexToDec + CLng(strEachCharFromstrHex) * 16 ^ (len(strHex) - 1)
    else
    HexToDec = 0
    Exit Do
    End If
    else
    HexToDec = HexToDec + lngHexFromAToF * 16 ^ (len(strHex) - 1)
    lngHexFromAToF = 0
    End If
    strHex = mid(strHex, 2, len(strHex))
    Loop
    End Function





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured