Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Concatenating an Integer to a String as an Integer 1

Status
Not open for further replies.

Genimuse

Programmer
Joined
May 15, 2003
Messages
1,797
Location
US
I'm creating a string that will be converted to base64. The string includes some text (standard string stuff) and some DWORDs. I need to concatenate the string along the lines of

text + DWORD + DWORD + text

A UInteger will hold a DWORD nicely (since a DWORD is an unsigned 32 bit int), but I can't figure out how to tack it onto the string in its true byte form, as opposed to a string representation of the number. That is, I don't want:

sample + 69 + 2383 + example

but rather

sample + 69 in its true 4-byte form + {b]2383 in its true 4-byte form[/b] + example

Does that make sense? I can't figure out how to concatenate the 4 bytes that are the uint version of the number instead of the string representation.
 
something like this?
Code:
    Private Sub s()
        Dim m As UInt32
        Dim b() As Byte
        b = System.BitConverter.GetBytes(m)
    End Sub

Christiaan Baes
Belgium

"Time for a new sig." - Me
 
Ah, that looks like what I'm looking for, at least partially. Unfortunately it only returns the last byte, so with, say, "69" it returns "69", and with 1113678437 it returns "101" (the final byte).

I wrote a little routine that I think works, but I'd like confirmation. This might be made easier with BitConverter, but I haven't thought it through.
Code:
Function dwordToString(ByVal numIn As UInteger) As String
  Dim place(4) As Byte
  Dim multiple(4) As UInteger
  Dim calc As UInteger
  Dim newString As String = ""
  Dim i, j As Integer

  For i = 3 To 0 Step -1
    multiple(i) = 2 ^ (8 * i)
    calc = numIn
    For j = i + 1 To 3
      calc -= (place(j) * multiple(j))
    Next
    place(i) = Math.Floor(calc / multiple(i))
    newString &= Chr(place(i))
  Next

  Return newString
End Function
So "69" would return Chr(0) & Chr(0) & Chr(0) & Chr(69), or "[Null][Null][Null]E"

And "1113678437" would return Chr(66) & Chr(97) & Chr(98) & Chr(101), or "Babe".

Those would be the byte equivalents of those numbers as respresented in a text file, right?
 
Part of my concern is BigEndian vs. LittleEndian stuff and such. I don't know how Windows would normally store an unsigned int in a file.
 
I'm beginning to doubt my sanity. A month of death march is making it pretty likely.

Here's what I'm trying to do. My probably-bizarre thinking is that in order to make that header part, I have to create a string with the 4-byte signature, then the five DWORDs, then my file name, all of which will have Convert.ToBase64String applied to it. Because this is just a hunk of XML in the end, I assume that the DWORDS must all be of identical size because otherwise the receiver wouldn't be able to parse the big chunk of Base64 text. That would seem to mean that I can't have one DWORD that is, say, "69", and another that is "1113678437", since there's no way it could know how to chop up the results. Hence the conversion thing above. But am I insane?
 
[problem]
We are currently unable to serve your request
We apologize, but an error occurred and your request could not be completed.

This error has been logged. If you have additional information that you believe may have caused this error please report the problem here.
[/problem]

Yes you are insane. But that's beside the point. You even forgot to tell me about my spelling.

and this

Code:
 Dim m As UInt32 = 69
        Dim b() As Byte
        b = System.BitConverter.GetBytes(m)
        TextBox1.Text = m.ToString
        For inttemp As Integer = 0 To b.Length - 1
            TextBox2.Text &= b(inttemp).ToString & " "
        Next
        m = 111111145
        b = System.BitConverter.GetBytes(m)
        TextBox3.Text = m.ToString
        For inttemp As Integer = 0 To b.Length - 1
            TextBox4.Text &= b(inttemp).ToString & " "
        Next

gave me this

69
69 0 0 0

111111145
233 107 159 6

so that's what you want if it is Uint it will always be four bytes and the other end will know this or will have to know it to interpret wich I think it will.

Christiaan Baes
Belgium

"Time for a new sig." - Me
 
Ah, perfect! That also indicates that it's likely BigEndian, which is good to know. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top