Results 1 to 23 of 23

Thread: asc()

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    45

    asc()

    I'm using vb to query a server, it returns a data packet which has several numbers in it, i can get vb to convert an int to ascii by using asc() but it dosent work for floats - instead of a number i get a box, i assume this means its an invalid ascii charcter. Does anyone know how to fix this?

  2. #2
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    Type-cast it to an int first.

    VB Code:
    1. Dim abcd As Single
    2. Dim efgh As String
    3.  
    4. abcd = 65.4
    5. efgh = Chr$(Int(abcd))
    6. 'efgh now equals "A"

  3. #3
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519
    There are a lot of characters that windows can not display. Characters like "LineFeed", "Carriage Return", "Escape", ect... are actions and not actually a visable symbol so windows will always put a a "box" or a "bar" to represent it. If you do a search on the ascii value in google you will find its value... or post the value and I can tel you what it is.. (I have a chart I use to look up ascii values)


    Rudy

    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    asc() will only work properly for integers, as each ascii character maps to an integer (in the range 0-255). if you pass a float to the asc() function, it will round it to an integer (but I dont know if it would round up or down).

    If you get a 'box' returned from the asc() function, it just means that there isn't a (display) character for it in the font you are using. This is most likely to happen for values under 32, as they are (historically) control codes, for new line, delete, etc.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    a quick thought.. are you trying to just convert the number into text? if so, just use CStr (Convert to STRing):

    VB Code:
    1. a = 5.3543
    2.  
    3. b =  Cstr(a)   'b now contains "5.3543"

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    45
    No i dont think its a control code because whats meant to come out is a number not a symbol. Perhaps if i explain it a bit more it might help?

    I'm receiveing string from the server, it contains charcters (which vb can understand without asc() ), ints (that need asc() to get their real value, and floats (which i need to find).

    Both the ints and floats are meant to be real numbers (they're just boxes when i view the string from the server) ie 4, 6.3243. However i believe (heres where im not sure) that they are transmitted in some type of binary. I believe that the charcters are automatically converted by vb but that it cant handle the ints and floats and needs to be told what to do?

    If it helps heres the site that im getting my info on the string thats been passed to the program http://dev.kquery.com/index.php?article=4 (the 3rd half life query - yyyyplayers)

    EDIT** just tried cstr- it just converts the float to 0 (i know what value is meant to be and its not 0 )
    Last edited by zil; Oct 22nd, 2003 at 09:05 AM.

  7. #7
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    Perhaps you could use CopyMemory API function, to copy the four bytes into a single precision float, or eight bytes into a double precision.

    VB Code:
    1. 'Put this at module level:
    2. Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    3.  
    4. 'Then use this:
    5. Dim abcd As Single
    6. Dim IncomingData() As Byte
    7.  
    8. 'Pretend the float is stored 16 bytes into the data...
    9.  
    10. CopyMemory (abcd, IncomingData(16), 4)

  8. #8
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Hmm

    Are you doing a query of a HL server?

    I did that about 6 months ago I think, I might still have the code, want me to send it to you ?

    Cheers!
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  9. #9
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    (sorry for the off topic post) Hey N30, why be you not on MSN or ICQ?

  10. #10

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    45
    I think we might be getting somewhere, the copymemory function does indeed convert it into a float however the value that it returns is the same for every time. ie no matter what the actual value of the float is copymemory retruns 1.65305E-39

  11. #11

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    45
    just saw ur post vbneo yea im trying to query a hl server. The part i cant get is converting a float (the persons time on the server), if u managed to do that in ur program id really apreciate it if i could get a look at it.

  12. #12
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    Can I see your call to CopyMemory, perhaps it returns the same number because you are pointing it to the wrong and/or same place each time.

  13. #13

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    45
    Well heres the whole function that im using for getting all the data (i realise that theres several innecessary variables there, i was using them for testing and might need them again)

    VB Code:
    1. Private Sub Playerinfo_DataArrival(ByVal bytesTotal As Long)
    2. Dim FileNo As Integer
    3. Dim TotalPlayers As Integer
    4. Dim i, c As Integer
    5. Dim temp1, temp2, temp3, temp4 As Integer
    6. i = 0
    7. c = 7
    8. FileNo = FreeFile
    9.  
    10. lst1.Clear
    11.  
    12. PlayerInfo.GetData strData$
    13.  
    14. TotalPlayers = Asc(Mid(strData, 6, 1))
    15. MsgBox "Amount of players: " & TotalPlayers
    16.  
    17. Do While i < TotalPlayers
    18.     i = i + 1
    19.     Player(i).Name = ""
    20.     Player(i).ID = Asc(Mid(strData, c, 1))
    21.     c = c + 1
    22.     While (Asc(Mid(strData, c, 1)) <> 0)
    23.         Player(i).Name = Player(i).Name + (Mid(strData, c, 1))
    24.         c = c + 1
    25.     Wend
    26.     c = c + 1
    27.     Player(i).Frags = Asc(Mid(strData, c, 4))
    28.    
    29.     c = c + 1
    30.     'insert time code here
    31.    
    32.     'Then use this:
    33.     Dim abcd As Single
    34.  
    35.  
    36. 'Pretend the float is stored 16 bytes into the data...
    37.  
    38.     CopyMemory abcd, Mid(strData, c, 7), 4
    39.     MsgBox abcd
    40.    
    41.     Player(i).Time = abcd
    42.    
    43.    
    44.     lst1.AddItem (Player(i).ID & " " & Player(i).Name & " " & Player(i).Frags & " " & Player(i).Time)
    45.     c = c + 7
    46.    
    47. Loop
    48. End Sub

  14. #14
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Hmm

    AFAIK I just send a string command to the server, and it threw all the data it had at me, a bit like IRC. I'm at work right now, but i'll look into it as soon as i get home.

    Cheers!
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  15. #15
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    Try this instead:

    VB Code:
    1. CopyMemory abcd, VarPtr(strData) + c, 4

    You can't pass CopyMemory immediate values, they must be locations in memory, and the Mid() function returns an immediate value.

  16. #16

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    45
    Well strdata actually contains a long list of charcters and ints and floats. Mid(strData, c, 7) gets the point in the string that contains the float. So i think u might have meant varptr(Mid(strData, c, 7))? Possibly not, i also tried assigning Mid(strData, c, 7) to a variable and then doing varptr() on that variable - either way i get the same number for all values of the float.

  17. #17
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    VB Code:
    1. Dim bytData() As Byte
    2. bytData = StrConv(strData, vbFromUnicode)
    3.  
    4. CopyMemory asdf, bytData(c), 4
    How about that?

    The Mid function returns an immediate string value, not a pointer to the sub-string, and CopyMemory only works if you pass it pointers.

  18. #18

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    45
    Well while that does give me differant values for the float they are all less than 0. Unless hl has a wierd way of handling time i would have expected the float to be greater than 0 and represent the number of seconds that the player has spent on the server.

  19. #19
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    Perhaps I am a byte off, maybe it's really bytData(c - 1) or bytData(c + 1)...

    I just noticed something... when you call Asc() in your code you are using a string that has more than one character. Asc() will only return the value for the first character in a string... so it will never exceed 255...

  20. #20

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    45
    fraid not, ive tried both and either way i get some minuses

  21. #21
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    *cries*

    I'm sure all floating points were stored the same... are you sure it's a floating point? Wouldn't it be really funny if it was just an integer?

    Haha, ah well... Hopefully vbNeo can help you... since you've got me completely stumped.

  22. #22

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    45
    lol im sorry i didnt mean to make u cry

    Well if it was an int asc() should work + the web site says it isnt. Hopefully vbneo'll be able to help me.


    Oh and about asc() only reading the first byte, it dosent really matter cause i seriously doubt that anyone will be able to get more than 255 frags

  23. #23

    Thread Starter
    Member
    Join Date
    Jul 2003
    Posts
    45
    ok vbneo i cant seem to reply to ur pm for some reason. But ur code cant really help me. U c ur querying the server for differant data than i am. Ur just getting the server info. Thats all sent to u as a string so that pretty straight forward to split up. However im querying the server for player info. That's sent as a series of characters integers and floats so it cant be handled in the same way that u were handling the data u recieved.

    im thinking that perhaps the reason asc() dosent work on the float is because like u said dreamlax asc only works for the first byte. And from what i can see from examples of how to query hl servers, ints only take up 1 byte, but the floats take up 4. So is there nayway i can perform asc() on a 4 byte float (by 4 bytes i mean all 4 bytes contain data other than 0 )

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