473,287 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Problem to write a good serie of bytes in a file.

Here is some lines of code than I wrote. You can copy/paste theis code as
code of form1 in a new project.
My problem is this one :
I try to write in a file a serie of bytes.
BUT some bytes written in file are not the sent bytes.
Copy and paste the following lines to observe my problem.
What can I do to resolve problem ?
Only System.Text.Encoding.ASCII write the same number of bytes, but not the
good bytes.
Someone can help me. Thanks by advance.

Public Class Form1
Dim TextBox1 As New TextBox
Dim TextBox2 As New TextBox
Dim Label1 As New Label
Dim Label2 As New Label
Dim Label3 As New Label
Dim Label4 As New Label

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Size = New System.Drawing.Size(800, 500)

Label1.Location = New System.Drawing.Size(20, 13)
Label1.Text = "Data to write"
Me.Controls.Add(Label1)

Label2.Location = New System.Drawing.Size(20, 40)
Label2.Text = "Data written"
Me.Controls.Add(Label2)

Label3.Location = New System.Drawing.Size(350, 13)
'Label3.Text = "Data written"
Me.Controls.Add(Label3)

Label4.Location = New System.Drawing.Size(350, 40)
'Label4.Text = "Data written"
Me.Controls.Add(Label4)

TextBox1.Size = New System.Drawing.Size(200, 20)
TextBox1.Location = New System.Drawing.Size(120, 13)
Me.Controls.Add(TextBox1)

TextBox2.Size = New System.Drawing.Size(200, 20)
TextBox2.Location = New System.Drawing.Size(120, 40)
Me.Controls.Add(TextBox2)

Dim button1 As New Button
button1.Text = "BinaryWriter encoding ASCII"
button1.Size = New System.Drawing.Size(200, 20)
button1.Location = New System.Drawing.Size(550, 13)
AddHandler button1.Click, AddressOf Button1_Click
Me.Controls.Add(button1)

End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim Temp As String = "ÿØÿá"
TextBox1.Text = Temp
Label3.Text = "Length to write : " & Temp.Length.ToString & "
octets)"
Dim fullFileName As String = "c:\converted music\test1.jpg"

Dim sw As System.IO.StreamWriter
sw = New System.IO.StreamWriter(fullFileName, False,
System.Text.Encoding.ASCII)
sw.Write(Temp)
sw.Close()

Dim sr As System.IO.StreamReader
sr = New System.IO.StreamReader(fullFileName,
System.Text.Encoding.ASCII)
Do While Not sr.EndOfStream
TextBox2.Text = TextBox2.Text & Chr(sr.Read())
Loop
Label4.Text = "Length written : " & TextBox2.Text.Length.ToString &
" octets)"
sr.Close()
End Sub
End Class

Feb 4 '06 #1
5 3620
Keep in mind that ASCII only contains codes from 0 to 127. The
characters you are attempting to write are not part of the ASCII
Encoding. You might try Encoding.Default or Encoding.UTF8 to see if
that helps. Or use the Encoding for the language you are trying to
write.

Hope this helps.

Feb 4 '06 #2
Thanks for your response.
I explain why I must do that.

I write a program in Visual Studio, in vb, bound to an Access Database.
Some memo field contains exact copy, byte to byte, of 'jpg' files, each file
beeing obviously shorter then 65 k.
What I search to do if to write on a file the bytes loaded in memo file.
I put bytes of memo file in a string and I try to write the bytes in a file
with 'jpg' extension.
If I succeed, I can use image.fromfile(jpgfile) to show the file in a
picture box.

BUT if I use encoding.ASCII, I loose all bytes greater than 127, and I keep
the same number of bytes than the number of bytes of the memo file (and
string). Obviously, And the jpgfile resulting doesn't work. because I lost
all the values greater than 127, replaces by chr63 ='?'

BUT if I use UTF8, bytes are coded in a such way that the binary values of
the file written are not the same, byte by byte, than the binary values of
the string because bytes greater than 127 are coded on 2 bytes, I think. And
the number of bytes is not the same. And the jpgfile resulting doesn't work.

What I search is a way to write all bytes in a file keeping the sale number
of bytes and writing on a byte the values between 0 and 256.
Is it possible ? If it is possible and you have solution, I'm the hapiest
man in the world.
-----------------------------------------
PS : In VBA of Access I can do that like this (and that functions perfectly)
:

Dim lFile1 As Long
Dim Temp1 as string

lFile1 = FreeFile
Open nomFileImageWithChemin For Binary As #lFile1
Put #lFile1, , temp
Close #lFile1
-------------------------------------------

Great thanks for your attention.

"Chris Dunaway" <du******@gmail.com> a écrit dans le message de news:
11*********************@g44g2000cwa.googlegroups.c om...
Keep in mind that ASCII only contains codes from 0 to 127. The
characters you are attempting to write are not part of the ASCII
Encoding. You might try Encoding.Default or Encoding.UTF8 to see if
that helps. Or use the Encoding for the language you are trying to
write.

Hope this helps.

Feb 5 '06 #3
> I write a program in Visual Studio, in vb, bound to an Access Database.
Some memo field contains exact copy, byte to byte, of 'jpg' files, each
file beeing obviously shorter then 65 k.
What I search to do if to write on a file the bytes loaded in memo file.
I put bytes of memo file in a string and I try to write the bytes in a
file with 'jpg' extension.
If I succeed, I can use image.fromfile(jpgfile) to show the file in a
picture box.

BUT if I use encoding.ASCII, I loose all bytes greater than 127, and I
keep the same number of bytes than the number of bytes of the memo file
(and string). Obviously, And the jpgfile resulting doesn't work. because I
lost all the values greater than 127, replaces by chr63 ='?'

BUT if I use UTF8, bytes are coded in a such way that the binary values of
the file written are not the same, byte by byte, than the binary values of
the string because bytes greater than 127 are coded on 2 bytes, I think.
And the number of bytes is not the same. And the jpgfile resulting doesn't
work.

What I search is a way to write all bytes in a file keeping the sale
number of bytes and writing on a byte the values between 0 and 256.
Is it possible ? If it is possible and you have solution, I'm the hapiest
man in the world.


Philip,

See if this code will increase your happiness :-). The secret is encoding
and decoding using UTF-16.

You will need:

Imports System.IO
Imports System.Text

a PictureBox named "PictureBox1", a button named "Button1" on your form and
a jpeg file named "In.jpg" in C:\Temp. This code doesn't do any error
handling but I think it will give you a good start.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

' Read a jpg into a byte array

Dim myBinaryReader As New BinaryReader(File.OpenRead("C:\Temp\In.jpg"))

Dim myByteArray As Byte() =
myBinaryReader.ReadBytes(myBinaryReader.BaseStream .Length)

myBinaryReader.Close()

' Encode the byte array using UTF-16 to a string for storage (in a memo
field?)

Dim myString As String

Dim myUTF16 As New UnicodeEncoding

myString = myUTF16.GetString(myByteArray)

' Decode the string using UTF-16 back to a byte array

myByteArray = myUTF16.GetBytes(myString)

' Convert the byte array to a MemoryStream

Dim myMemoryStream As MemoryStream

myMemoryStream = New MemoryStream(myByteArray, 0, myByteArray.Length)

myMemoryStream.Write(myByteArray, 0, myByteArray.Length)

' Display the image from the MemoryStream

PictureBox1.Image = Image.FromStream(myMemoryStream, True)

' Write the byte array to a new disk file

Dim myBinaryWriter As New BinaryWriter(File.OpenWrite("C:\Temp\Out.jpg"))

myBinaryWriter.Write(myByteArray)

myBinaryWriter.Close()

End Sub

Stan

Stan Smith
ADS Programming Services
2320 Highland Avenue South
Suite 290
Birmingham, AL 35205
205-222-1661
www.adsprogramming.com
ssmith_at_adsprogramming.com
Feb 5 '06 #4
Thanks for your interest.

1.
I launch you routine, but I have an error on
PictureBox1.Image = Image.FromStream(myMemoryStream, True)
The error is 'Parameter is not valid'. Do you know why ?

2.
I disactivated this problematic line only to see if the remaining part
functions well.
And there is a another problem.
When BinaryReader read 'in.jpg', the array myByteArray is 25864 long (It's
good, it's the length of 'in.jpg').
But when UTF16 codes myByteArray, myByteArray loses some bytes and this
array has now only 25092 elements. So 'out.jpg' is only 25092 bytes long.

And 'in.jpg' is good and visible in pictureBox, but not 'out.jpg'.

I'm a very happy because you approach from truth ands you have interest for
my problem. Can you spend again a lot of time to study these problems ?
Thanks by advance if it's possible.


"Stan Smith" <ss****@adsprogramming.com> a écrit dans le message de news:
%2****************@TK2MSFTNGP12.phx.gbl...
I write a program in Visual Studio, in vb, bound to an Access Database.
Some memo field contains exact copy, byte to byte, of 'jpg' files, each
file beeing obviously shorter then 65 k.
What I search to do if to write on a file the bytes loaded in memo file.
I put bytes of memo file in a string and I try to write the bytes in a
file with 'jpg' extension.
If I succeed, I can use image.fromfile(jpgfile) to show the file in a
picture box.

BUT if I use encoding.ASCII, I loose all bytes greater than 127, and I
keep the same number of bytes than the number of bytes of the memo file
(and string). Obviously, And the jpgfile resulting doesn't work. because
I lost all the values greater than 127, replaces by chr63 ='?'

BUT if I use UTF8, bytes are coded in a such way that the binary values
of the file written are not the same, byte by byte, than the binary
values of the string because bytes greater than 127 are coded on 2 bytes,
I think. And the number of bytes is not the same. And the jpgfile
resulting doesn't work.

What I search is a way to write all bytes in a file keeping the sale
number of bytes and writing on a byte the values between 0 and 256.
Is it possible ? If it is possible and you have solution, I'm the hapiest
man in the world.


Philip,

See if this code will increase your happiness :-). The secret is encoding
and decoding using UTF-16.

You will need:

Imports System.IO
Imports System.Text

a PictureBox named "PictureBox1", a button named "Button1" on your form
and a jpeg file named "In.jpg" in C:\Temp. This code doesn't do any error
handling but I think it will give you a good start.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

' Read a jpg into a byte array

Dim myBinaryReader As New BinaryReader(File.OpenRead("C:\Temp\In.jpg"))

Dim myByteArray As Byte() =
myBinaryReader.ReadBytes(myBinaryReader.BaseStream .Length)

myBinaryReader.Close()

' Encode the byte array using UTF-16 to a string for storage (in a memo
field?)

Dim myString As String

Dim myUTF16 As New UnicodeEncoding

myString = myUTF16.GetString(myByteArray)

' Decode the string using UTF-16 back to a byte array

myByteArray = myUTF16.GetBytes(myString)

' Convert the byte array to a MemoryStream

Dim myMemoryStream As MemoryStream

myMemoryStream = New MemoryStream(myByteArray, 0, myByteArray.Length)

myMemoryStream.Write(myByteArray, 0, myByteArray.Length)

' Display the image from the MemoryStream

PictureBox1.Image = Image.FromStream(myMemoryStream, True)

' Write the byte array to a new disk file

Dim myBinaryWriter As New BinaryWriter(File.OpenWrite("C:\Temp\Out.jpg"))

myBinaryWriter.Write(myByteArray)

myBinaryWriter.Close()

End Sub

Stan

Stan Smith
ADS Programming Services
2320 Highland Avenue South
Suite 290
Birmingham, AL 35205
205-222-1661
www.adsprogramming.com
ssmith_at_adsprogramming.com

Feb 5 '06 #5
I am the happiest man in the world !

Your routine functions ! Just I cut some lines :

Dim myBinaryReader As New BinaryReader(File.OpenRead("C:\in.jpg"))

Dim myByteArray As Byte() =
myBinaryReader.ReadBytes(myBinaryReader.BaseStream .Length)
myBinaryReader.Close()

' Convert the byte array to a MemoryStream
Dim myMemoryStream As MemoryStream
myMemoryStream = New MemoryStream(myByteArray, 0,
myByteArray.Length)
myMemoryStream.Write(myByteArray, 0, myByteArray.Length)
Debug.Print(myMemoryStream.Length)
' Display the image from the MemoryStream
PictureBox1.Image = Image.FromStream(myMemoryStream, True)

' Write the byte array to a new disk file
Dim myBinaryWriter As New BinaryWriter(File.OpenWrite("C:\out.jpg"))
myBinaryWriter.Write(myByteArray)
myBinaryWriter.Close()

Any conversion ASCII ou UTF16

Just one thing : in your routine you begin with a filestream, but I begin
with a string.
I am obliged to transform strin in Array of bytes without any encoder.
I do that :

Dim strImage As String = row.Cells("picture_field").Value.ToString
Dim myByteArray(strImage.Length) As Byte
Dim i As Integer
For i = 0 To strImage.Length - 1
myByteArray(i) = Asc(strImage.Chars(i))
Next
Do you know, to finish to resolve my problem, a better way to do that ?
I am sure that a function exists but I don't know which.
It will be my last question.

Thanks four your help.

Philip




"Stan Smith" <ss****@adsprogramming.com> a écrit dans le message de news:
%2****************@TK2MSFTNGP12.phx.gbl...
I write a program in Visual Studio, in vb, bound to an Access Database.
Some memo field contains exact copy, byte to byte, of 'jpg' files, each
file beeing obviously shorter then 65 k.
What I search to do if to write on a file the bytes loaded in memo file.
I put bytes of memo file in a string and I try to write the bytes in a
file with 'jpg' extension.
If I succeed, I can use image.fromfile(jpgfile) to show the file in a
picture box.

BUT if I use encoding.ASCII, I loose all bytes greater than 127, and I
keep the same number of bytes than the number of bytes of the memo file
(and string). Obviously, And the jpgfile resulting doesn't work. because
I lost all the values greater than 127, replaces by chr63 ='?'

BUT if I use UTF8, bytes are coded in a such way that the binary values
of the file written are not the same, byte by byte, than the binary
values of the string because bytes greater than 127 are coded on 2 bytes,
I think. And the number of bytes is not the same. And the jpgfile
resulting doesn't work.

What I search is a way to write all bytes in a file keeping the sale
number of bytes and writing on a byte the values between 0 and 256.
Is it possible ? If it is possible and you have solution, I'm the hapiest
man in the world.


Philip,

See if this code will increase your happiness :-). The secret is encoding
and decoding using UTF-16.

You will need:

Imports System.IO
Imports System.Text

a PictureBox named "PictureBox1", a button named "Button1" on your form
and a jpeg file named "In.jpg" in C:\Temp. This code doesn't do any error
handling but I think it will give you a good start.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

' Read a jpg into a byte array

Dim myBinaryReader As New BinaryReader(File.OpenRead("C:\Temp\In.jpg"))

Dim myByteArray As Byte() =
myBinaryReader.ReadBytes(myBinaryReader.BaseStream .Length)

myBinaryReader.Close()

' Encode the byte array using UTF-16 to a string for storage (in a memo
field?)

Dim myString As String

Dim myUTF16 As New UnicodeEncoding

myString = myUTF16.GetString(myByteArray)

' Decode the string using UTF-16 back to a byte array

myByteArray = myUTF16.GetBytes(myString)

' Convert the byte array to a MemoryStream

Dim myMemoryStream As MemoryStream

myMemoryStream = New MemoryStream(myByteArray, 0, myByteArray.Length)

myMemoryStream.Write(myByteArray, 0, myByteArray.Length)

' Display the image from the MemoryStream

PictureBox1.Image = Image.FromStream(myMemoryStream, True)

' Write the byte array to a new disk file

Dim myBinaryWriter As New BinaryWriter(File.OpenWrite("C:\Temp\Out.jpg"))

myBinaryWriter.Write(myByteArray)

myBinaryWriter.Close()

End Sub

Stan

Stan Smith
ADS Programming Services
2320 Highland Avenue South
Suite 290
Birmingham, AL 35205
205-222-1661
www.adsprogramming.com
ssmith_at_adsprogramming.com

Feb 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: chris | last post by:
I'm writing a small app to help me learn more about cryptography. All it does is encrypt all of the files in directory A, and put the encrypted versions of the files in directory B. It then...
5
by: Juho Saarikko | last post by:
I made a Python script which takes Usenet message bodies from a database, decodes uuencoded contents and inserts them as Large Object into a PostGreSQL database. However, it appears that the to...
7
by: Bob | last post by:
Hi, I am trying to use BULK INSERT with format file. All of our data has few bytes of header in the data file which I would like to skip before doing BULK INSERT. Is it possible to write...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
3
by: Eugene | last post by:
I'm trying to write a class which uses BinaryWriter as its base but allows for queuing of write requests Public Class QueuedBinaryWriter Inherits BinaryWriter I override all the Write methods...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
3
by: =?Utf-8?B?TG9yZW4=?= | last post by:
I’m trying to encrypt and decrypt a file in vb.net. I am using the TripleDESCryptoServiceProvider encryption found in System.Security.Cryptography. Below is the code for my Encrypt and Decrypt...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.