String concatenation having null character

M

marco

Hi to all

I'm trying to concatenate a array of bytes (0-255) to a single string,
like this:

Private conteudoBytes(2000) As Byte
Private conteudoStr As String

When I try to do:

For i = 0 To 2000
conteudoStr = conteudoStr & Chr(conteudoBytes(i))
Next i

conteudoStr have the concatenated string to the first null character
(0x00), exclusively. I don't know why it stops there.
conteudoBytes(2000) have the contents of a binary file. Am i doing
this the wrong way?

Sincere regards and thank you in advance for reading,
Marco
 
T

Tom Ogilvy

Maybe something like this:

Private conteudoBytes(2000) As Byte
Private conteudoStr*2001 As String



For i = 0 To 2000
if conteudoBytes(i) <> 0 then
Mid(conteudoStr,i+1,1) = Chr(conteudoBytes(i))
end if
Next i
 
J

John Coleman

Are you sure that it stops there? If you were testing things by the
message box function, the msgbox function won't display past a null
character - but the data is there in the string nevertheless:

Sub test()
Dim S As String
S = "Hello" & Chr(0) & "World"
Debug.Print S 'prints "Hello World" in the immediate window
MsgBox S 'just displays "Hello"
MsgBox Mid(S, 7) 'displays "World" - the data is there and still
extractable
MsgBox Len(S) 'displays 11
End Sub

Hth

-John Coleman
 
J

John Coleman

Are you sure that it stops there? If you were testing things by the
message box function, the msgbox function won't display past a null
character - but the data is there in the string nevertheless:

Sub test()
Dim S As String
S = "Hello" & Chr(0) & "World"
Debug.Print S 'prints "Hello World" in the immediate window
MsgBox S 'just displays "Hello"
MsgBox Mid(S, 7) 'displays "World" - the data is there and still
extractable
MsgBox Len(S) 'displays 11
End Sub

Hth

-John Coleman
 
M

marco

That's it !!

MsgBox() doesn't show chr(0), but the Immediate Window does.

Thanks alot, you saved me of alot of (debugging) time.

Thanks to all of you.

Marco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top