Jade_Emeperor


Hi. I'm currently writing a program for RFID using Visual Basic 6.0

But however I'm not sure how I can convert my decimal string to Chr$(). Because I currently wrote the code like this:

Code Snippet

decimal_string = CInt("&H" & hex_string)

Char_String = Chr$(decimal_string)

And an error pop up telling me that is an invalid procedure or arguement. Is there any code I have to add in between this follwing code

Thanks for the help.




Re: Convert decimal string to Chr$()

ADG


Hi

The Chr$ function accepts values between 0 and 255, if Decimal_string is greater than 255 you will get this error. Chr$ returns the ASCII character represented by the integer that is passed. If you want to convert decimal_string to text use the str$ function.

Regards

ADG






Re: Convert decimal string to Chr$()

sjoo

you need a function to convert hex into dec value.

and chr() function requires long-type parameter , not string.

if i rewrite your code, i'd like to write as following:

Dim decimal_string as long

decimal_string=Hex2Dec(hex_string)

char_string=Chr(decimal_string)

Function Hex2Dec(n1 As String) As Long
Dim nl1 As Long
Dim nGVal As Long
Dim nSteper As Long
Dim nCount As Long
Dim x As Long
Dim nVal As Long
Dim Stepit As Long
Dim hVal As String

nl1 = Len(n1)
nGVal = 0
nSteper = 16
nCount = 1
For x = nl1 To 1 Step -1
hVal = UCase(Mid$(n1, x, 1))
Select Case hVal
Case "A"
nVal = 10
Case "B"
nVal = 11
Case "C"
nVal = 12
Case "D"
nVal = 13
Case "E"
nVal = 14
Case "F"
nVal = 15
Case Else
nVal = Val(hVal)
End Select
Stepit = (nSteper ^ (nCount - 1))
nGVal = nGVal + nVal * Stepit
nCount = nCount + 1
Next x
Hex2Dec = nGVal
End Function






Re: Convert decimal string to Chr$()

Jade_Emeperor

Hi.

I understand what you meant. But however, I do really need to convert decimal string to Chr$() because of this code:

Code Snippet

MSComm1.Output = Chr$(&H11) + Chr$(&HFF) + Chr$(&HB0) + Chr$(&H23) & _

Chr$(&H1) + tagID_ChrStr + Chr$(&H0) + Chr$(&H1) + Char_String

This code is used to send out the following data in Chr$() and by rights the reader should reply back with the tag content (whatever that is being written into the tag). Never mind about the tagID_ChrStr. It's the Char_String which I'm having problems with and it's not the same code as tagID_ChrStr.

Thanks





Re: Convert decimal string to Chr$()

Jade_Emeperor

sjoo wrote:

you need a function to convert hex into dec value.

and chr() function requires long-type parameter , not string.

So what you meant is that I cannot use decimal_string = CInt("&H" & hex_string)

EDIT: I'll try out your code.

EDIT2: I've tried out your code. It can able to convert Hex to Decimal successfully. But when it comes to char_string = Chr(decimal_string), it still gives me the same error.

read the above post before the post which contains the Hex2Dec function you've posted. That's is why I need to use Chr$()





Re: Convert decimal string to Chr$()

ADG

Hi again

Your original code works fine until the value of decimal_string is greater than 255. I tested the code in a module, if you run the below:

Sub test()
Dim decimal_string, hex_string
hex_string = "45"
decimal_string = CInt("&H" & hex_string)
Debug.Print decimal_string
Debug.Print Chr$(decimal_string)
End Sub

you get the below in the debug window.

69
E

If you change the value of Hex_string to 171 or above you get your error





Re: Convert decimal string to Chr$()

ADG

Interestingly the error is caused by the assignment not the function. It would appear setting a string to a Chr$ value above 255 is not possible. E,g, the first line below will error, the second will not!

decimal_string = Chr$(258)

debug.print Chr$(258)





Re: Convert decimal string to Chr$()

Jade_Emeperor

Thanks. I'll try working on the codes you've suggested next Monday as I didn't bring my project along back home.



Re: Convert decimal string to Chr$()

Jade_Emeperor

EDIT: It's ok now. I adjust the code slightly and I manage to get it working. The code as follows:

Assume "y = 1F", "z = 21"

Code Snippet

crcbuffer = Chr$("&H" & y) + Chr$("&H" & z)

Many thanks for helping me out once again.