|
-
Oct 22nd, 2003, 08:11 AM
#1
Thread Starter
Member
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?
-
Oct 22nd, 2003, 08:50 AM
#2
Hyperactive Member
Type-cast it to an int first.
VB Code:
Dim abcd As Single
Dim efgh As String
abcd = 65.4
efgh = Chr$(Int(abcd))
'efgh now equals "A"
-
Oct 22nd, 2003, 08:53 AM
#3
Frenzied Member
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".
-
Oct 22nd, 2003, 08:54 AM
#4
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.
-
Oct 22nd, 2003, 08:57 AM
#5
a quick thought.. are you trying to just convert the number into text? if so, just use CStr (Convert to STRing):
VB Code:
a = 5.3543
b = Cstr(a) 'b now contains "5.3543"
-
Oct 22nd, 2003, 09:01 AM
#6
Thread Starter
Member
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.
-
Oct 22nd, 2003, 09:07 AM
#7
Hyperactive Member
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:
'Put this at module level:
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
'Then use this:
Dim abcd As Single
Dim IncomingData() As Byte
'Pretend the float is stored 16 bytes into the data...
CopyMemory (abcd, IncomingData(16), 4)
-
Oct 22nd, 2003, 09:08 AM
#8
Frenzied Member
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.
-
Oct 22nd, 2003, 09:09 AM
#9
Hyperactive Member
(sorry for the off topic post) Hey N30, why be you not on MSN or ICQ?
-
Oct 22nd, 2003, 09:12 AM
#10
Thread Starter
Member
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
-
Oct 22nd, 2003, 09:14 AM
#11
Thread Starter
Member
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.
-
Oct 22nd, 2003, 09:18 AM
#12
Hyperactive Member
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.
-
Oct 22nd, 2003, 09:30 AM
#13
Thread Starter
Member
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:
Private Sub Playerinfo_DataArrival(ByVal bytesTotal As Long)
Dim FileNo As Integer
Dim TotalPlayers As Integer
Dim i, c As Integer
Dim temp1, temp2, temp3, temp4 As Integer
i = 0
c = 7
FileNo = FreeFile
lst1.Clear
PlayerInfo.GetData strData$
TotalPlayers = Asc(Mid(strData, 6, 1))
MsgBox "Amount of players: " & TotalPlayers
Do While i < TotalPlayers
i = i + 1
Player(i).Name = ""
Player(i).ID = Asc(Mid(strData, c, 1))
c = c + 1
While (Asc(Mid(strData, c, 1)) <> 0)
Player(i).Name = Player(i).Name + (Mid(strData, c, 1))
c = c + 1
Wend
c = c + 1
Player(i).Frags = Asc(Mid(strData, c, 4))
c = c + 1
'insert time code here
'Then use this:
Dim abcd As Single
'Pretend the float is stored 16 bytes into the data...
CopyMemory abcd, Mid(strData, c, 7), 4
MsgBox abcd
Player(i).Time = abcd
lst1.AddItem (Player(i).ID & " " & Player(i).Name & " " & Player(i).Frags & " " & Player(i).Time)
c = c + 7
Loop
End Sub
-
Oct 22nd, 2003, 09:31 AM
#14
Frenzied Member
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.
-
Oct 22nd, 2003, 09:37 AM
#15
Hyperactive Member
Try this instead:
VB Code:
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.
-
Oct 22nd, 2003, 09:48 AM
#16
Thread Starter
Member
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.
-
Oct 22nd, 2003, 09:54 AM
#17
Hyperactive Member
VB Code:
Dim bytData() As Byte
bytData = StrConv(strData, vbFromUnicode)
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.
-
Oct 22nd, 2003, 10:01 AM
#18
Thread Starter
Member
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.
-
Oct 22nd, 2003, 10:18 AM
#19
Hyperactive Member
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...
-
Oct 22nd, 2003, 10:24 AM
#20
Thread Starter
Member
fraid not, ive tried both and either way i get some minuses
-
Oct 22nd, 2003, 10:27 AM
#21
Hyperactive Member
*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.
-
Oct 22nd, 2003, 10:31 AM
#22
Thread Starter
Member
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
-
Oct 23rd, 2003, 12:56 PM
#23
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|