Converting an Integer to String

Im trying to convert some integers to a string (which works fine)

For example

My Funciton MKI is working ok as far as i can tell

Dim v1 as String = MKI(116)

V1 then equals "t

I wish it to equal "t"

Can anyone offer any suggestions please



Answer this question

Converting an Integer to String

  • bfarr23

    Use Space(1) to avoid having to use SubString().

    This code could fail when you least expect it. Some bad, rainy day, the garbage collector is going to move sTemp in memory just as CopyMemory() is executing. VB.NET has a good alternative to MKI():
    Dim V1 As String = Chr(116)



  • 00Negative

    I was pretty mystified what you were trying to do until I found this webpage. I'm not surprised these functions were dropped from the language, they make nasty assumptions about the internal structure of a string, assumptions that stopped working properly somewhere around VB4. I can only recommend you completely remove the use of these functions; show us some code that uses them and we'll help you clean it up.



  • Joseph Stalin

    Oh boy, you're in for a lot of trouble. Strings now also have an encoding. And .NET doesn't like dealing with Chr(0). The encoding kicks in when a string character has a value >= 128. There are many different kind of encodings available. The simplest is System.Text.Encoding.ASCII. It won't handle character codes >= 128, it just replaces them with " ". A standard encoding like UTF8 encodes high values with multiple characters. It's dangerous to say: "it can't be done" but I have to say: it can't be done.

    I'm going to unsubscribe from this thread and wish you the best of luck.



  • Stckmn3

    Thats good to know nobugz :)

    When i have the app "completed" i might have to go back and adjust all the code to use proper .net bits

    Don't suppose you know the .net bits for

    MKI

    MKL

    CVI

    CVL

    CVD

    :)

    Private Declare Sub CopyMemoryMKL Lib "Kernel32" Alias "RtlMoveMemory" (ByVal hDest As String, ByRef hSource As Integer, ByVal iBytes As Integer)

    Private Declare Sub CopyMemoryMKI Lib "Kernel32" Alias "RtlMoveMemory" (ByVal hDest As String, ByRef hSource As Short, ByVal iBytes As Integer)

    Private Declare Sub CopyMemoryCVI Lib "Kernel32" Alias "RtlMoveMemory" (ByRef hDest As Short, ByVal hSource As String, ByVal iBytes As Integer)

    Private Declare Sub CopyMemoryCVDt Lib "Kernel32" Alias "RtlMoveMemory" (ByRef hDest As Double, ByVal hSource As String, ByVal iBytes As Integer)

    Private Declare Sub CopyMemoryCVL Lib "Kernel32" Alias "RtlMoveMemory" (ByRef hDest As Integer, ByVal hSource As String, ByVal iBytes As Integer)


  • Leonard Lee

    For what your doing your should either using the .tostring method or using a conversion function like Cint, Ctype( , STring)

    To convert an integer to a string, sure with option strict off then there will be an implicit conversion but its best to make this conversion explicit and specific.

    Without seeing what you Function MKI is actually doing its rather difficult to tell what your problem is.

    Turn Option strict on and make sure that you are specifying any conversions.


  • Thymen

    Kinda, a propietary date format to save a few bytes. You can use Chr() to go from a 16-bit integer to a string and Asc() to go back. However, the generated string would not be the same, it would just have one character in it. This is only a problem if you need to read data that the old program generated...



  • Can-Ann

    Only use RtlMoveMemory on unmanaged memory locations. Even then, there are some .NET functions which offer better handling (Marshal) of such copies/moves.

    All these declarations are doing is moving at most 4 bytes around. Not sure what you are actually trying to achieve, but look at the Marshal.Copy (as a generic alternative), and associated support functions for locking memory in place. You are probably doing something which has a direct .NET equivalent, though. It just sounds like you are converting strings to numbers and visa versa.



  • jaimlin

    yeah i do need to read the old data (at this stage anyway)
  • QuantumMischief

    Thanks nobugz. :)

    It appears that in the short term at least, i will be forced to use the "unsafe code". Not a pleasant thought but i'll just cross my fingers, stick everything in a try loop and log all exceptions that may or may not occur.

    I'll post something if i find anything :)


  • Colmeister

    I wouldn't be surprised. This application is a progression of something that was originally started in QB7 and then moved to vb6, now some aspects are being used in .Net.

    I take a date (lets say its 08/05/50)

    The following code converts the above date into the integer

    and i would call it with the following line from somewhere in my code

    fgXs = TempX   '(tempx = "08/05/50")

    FromDate(1, fgXd, fgXs, fgx)

    Private Sub FromDate(ByVal gTx As Integer, ByVal tgXd As Double, ByVal tgXs As String, ByVal tgX As Integer)

    ' tempx is the date we want to change

    Tempx = tgXs

    Year = Mid$(tgXs$, 7)

    If Len(Year) = 2 Then

    If Val(Year) < 70 Then

    Year = "20" + Year

    Else

    Year = "19" + Year

    End If

    tgXs$ = Mid$(tgXs$, 1, 6) + Year

    End If

    Tempx$ = Mid$(tgXs$, 1, 2) + Mid$(tgXs$, 4, 2) + Mid$(tgXs$, 7, 4)

    Dd = Val(Microsoft.VisualBasic.Strings.Left$(Tempx$, 2)) : If Dd < 1 Or Dd > 31 Then GoTo formerror

    Mm = Val(Mid$(Tempx$, 3, 2)) : If Mm > 12 Then GoTo formerror

    If Mm < 1 Then Mm = Val(Mid$(Dt$, 4, 2))

    Yy = Val(Microsoft.VisualBasic.Strings.Right$(Tempx$, 4))

    If Yy < 1 Then Yy = Val(Mid$(Dt$, 7, 4))

    If Yy < 1972 Then GoTo formerror

    Yy = Yy - 1900

    x = Dd + (Yy - 72) * 365 + Int((Yy - 69) / 4) + Val(Mid$(Dq$, 3 * Mm - 2, 3))

    If Mm > 2 And (1900 + Yy) / 4 = Int((1900 + Yy) / 4) Then x = x + 1

    tgXd = x

    tgX = CInt(x)

    BdateW = tgX

    DTW = MKI(tgX)

    GoTo edate

    formerror:

    tgXs$ = "ERROR " : gTx = 0

    edate:

    End Sub ' Converts Date

    depending on the date field in question, i am either writing a date value as a integer OR as an integer converted to string

    In the above example, we'd end up with 08/05/50 =

    bdateW  = tgx = 28618 (integer)

    DTW = MKI(tgX) =  "Eo" as a string

    All the other functions are simply to convert the 28618 or the  Eo back into their original format

    Does that all make sense

     

     


  • Raihan Iqbal

    For interests sake this is the MKI function

    Private Declare Sub CopyMemoryMKI Lib "Kernel32" Alias "RtlMoveMemory" (ByVal hDest As String, ByRef hSource As Short, ByVal iBytes As Integer)

    Shared Function MKI(ByRef Value As Short) As String

    Dim sTemp As String = Space(2)

    CopyMemoryMKI(sTemp, Value, 2I)

    Return sTemp

    End Function

    as it turns out, all i had to do was change my code to read

    Dim V1 as String = MKI(116).substring(0,1)

    and that sorted my problem. Possibly not the safest thing to do, but it works :)

     


  • Converting an Integer to String