Re: StreamWriter and BinaryWriter
- From: "Armin Zingler" <az.nospam@xxxxxxxxxx>
- Date: Fri, 3 Feb 2006 17:10:09 +0100
"philip" <phil@xxxxxxxxxxxx> schrieb
If I execute that :
Dim Temp as string = "This is a text"
Dim sw As StreamWriter
Dim fullFileName as string = "c:\text.txt"
sw = New StreamWriter(fullFilename)
sw.Write(temp)
sw.Close()
the resulting file 'Text.txt' has the same length than the string
'temp'. Idem if I Use BinayWriter.
BUT if 'temp' contains others characters than readable character,
then the result file has not the same length than the string.
So, if temp contains "ÿØÿá" (that is to say a string concanated
with the 4 characters chr(255) & chr(216) & chr(255) and chr(225)),
the file writen is 8 octets long, and not 4 octets like the string
(twice more in the file than in the string). Consequently, the copy
of the memo field in a file to rebuild the 'img' file is wrong and
image reconstructed is not visible. In Access with the method "open
for binary", all is good.
So I think that I don't use the good method in Visual Studio 2005 to
copy this string on a file without any change.
Can someone help me and tell me the methos to do that ?
Strings are stored as Unicode characters. Each character occupies 2 bytes. If you open the Streamwriter without specifying an encoding, it uses UTF-8 encoding. This means that some characters are stored as 1 byte per character and some as 2 bytes per character. In your example, all characters are stored as 2 bytes per character.
Specify a 1-byte-encoding if you want to do so. Usually System.Text.Encoding.Default is used:
sw = New StreamWriter(fullFilename, true, System.Text.Encoding.Default)
Be aware that conversion from a 2-byte character set to a 1-byte character set can lead to information loss. Thus, you should use exactly the same encoding to write the file as it has been used to read it.
Armin
.
- Follow-Ups:
- Re: StreamWriter and BinaryWriter
- From: philip
- Re: StreamWriter and BinaryWriter
- References:
- StreamWriter and BinaryWriter
- From: philip
- StreamWriter and BinaryWriter
- Prev by Date: Re: Dynamic controls, menuitems, windowlist
- Next by Date: Re: some ways to do this
- Previous by thread: StreamWriter and BinaryWriter
- Next by thread: Re: StreamWriter and BinaryWriter
- Index(es):
Relevant Pages
|