473,399 Members | 3,832 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,399 software developers and data experts.

Why am I getting this?

Why does the following code produce a string ("Byte me!") in the output
file, rather than byte data as integers (66 121 116 101 32 109 101 33)?

Dim fs As System.IO.FileStream = System.IO.File.Open("C:\myMessage.dat",
IO.FileMode.Create)
Dim msg As String = "Byte me!"
Dim msgByteArray() As Byte = System.Text.Encoding.Default.GetBytes(msg)
fs.Write(msgByteArray, 0, msgByteArray.Length)

....And, how do I get the output file to have the byte array data?

PS - I know I could use a different Stream type, but I want to do this using
a FileStream.

Feb 27 '08 #1
4 1048


"Scott M." wrote:
Why does the following code produce a string ("Byte me!") in the output
file, rather than byte data as integers (66 121 116 101 32 109 101 33)?

Dim fs As System.IO.FileStream = System.IO.File.Open("C:\myMessage.dat",
IO.FileMode.Create)
Dim msg As String = "Byte me!"
Dim msgByteArray() As Byte = System.Text.Encoding.Default.GetBytes(msg)
fs.Write(msgByteArray, 0, msgByteArray.Length)

...And, how do I get the output file to have the byte array data?

PS - I know I could use a different Stream type, but I want to do this using
a FileStream.
It does show those values. I'm guessing you are using a text editor rather
than something like Notepad++ that can show the values as bytes. The bytes
you want and the string you got are the same.
Feb 27 '08 #2
As Mike says, you are actually getting the result you want.

However, you could try a BinaryWriter if you want to write other data types
(e.g. Integer) in binary format.


--
David Streeter
Synchrotech Software
Sydney Australia
"Scott M." wrote:
Why does the following code produce a string ("Byte me!") in the output
file, rather than byte data as integers (66 121 116 101 32 109 101 33)?

Dim fs As System.IO.FileStream = System.IO.File.Open("C:\myMessage.dat",
IO.FileMode.Create)
Dim msg As String = "Byte me!"
Dim msgByteArray() As Byte = System.Text.Encoding.Default.GetBytes(msg)
fs.Write(msgByteArray, 0, msgByteArray.Length)

...And, how do I get the output file to have the byte array data?

PS - I know I could use a different Stream type, but I want to do this using
a FileStream.
Feb 27 '08 #3
Scott,

I am not sure, however for one thing I am sure, your default text encoding
is not ASCII while you are showing us here a decimal representation of bit
values in 7 bit (bit)words confirm ASCII.

Cor

"Scott M." <sm**@nospam.nospamschreef in bericht
news:09**********************************@microsof t.com...
Why does the following code produce a string ("Byte me!") in the output
file, rather than byte data as integers (66 121 116 101 32 109 101 33)?

Dim fs As System.IO.FileStream =
System.IO.File.Open("C:\myMessage.dat", IO.FileMode.Create)
Dim msg As String = "Byte me!"
Dim msgByteArray() As Byte = System.Text.Encoding.Default.GetBytes(msg)
fs.Write(msgByteArray, 0, msgByteArray.Length)

...And, how do I get the output file to have the byte array data?

PS - I know I could use a different Stream type, but I want to do this
using a FileStream.
Feb 27 '08 #4
Hello Scott,

Apart from Mike, Davud and Cor's suggestions, if you expect to see the
integers (66 121 ...) in the output file, we can do it in this way:

Dim fs As System.IO.FileStream =
System.IO.File.Open("C:\myMessage.dat", IO.FileMode.Create)
Dim msg As String = "Byte me!"
Dim msgByteArray() As Byte =
System.Text.Encoding.Default.GetBytes(msg)
For Each bt As Byte In msgByteArray
Dim tmpByteArray() As Byte =
System.Text.Encoding.Default.GetBytes(bt.ToString( ))
fs.Write(tmpByteArray, 0, tmpByteArray.Length)
Next
fs.Close()

Hope it helps,

If you have any other concerns or questions, please feel free to let me
know.

Regards,
Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 27 '08 #5

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

Similar topics

2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
8
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported...
6
by: Jack Duijf | last post by:
Hello, I am looking for a person in The Netherlands that is willing to help me getting started with Vb.net. Please send a message to jack@aicn.nl if you can help me getting started with the...
0
by: Si | last post by:
I'm writing an HttpModule to intecept web method calls and retrieve custom authentication information from their SOAP headers. I have the httpModules tag in web.config and that appears to be...
4
by: R.Manikandan | last post by:
Hi In my code, one string variable is subjected to contain more amount of characters. If it cross certain limit, the string content in the varabile is automatically getting truncated and i am...
1
by: jm.suresh | last post by:
In the following code, I could not find out why the set and get methods are not called once I set the property. .... def __init__(self): .... self._color = 12 .... def...
2
by: soma.gunasekaran | last post by:
Hi All , I've stored the Image file..... But i want to retriving the Image files from MSACCSS 2003 (JPEG,bmp,Gif and etc....) So Pls help me........ help me............... Thanking you,...
3
by: wolverine | last post by:
Hi, I am injecting a javascript code into html pages and attaching some dom events to some elements via attachEvent (IE only). I just want to know that is there any chance by which my event...
7
by: alphasahoo | last post by:
Hi I am working on a program which writes the output a SQL select statements from number of source tables first to a load matrix and then writes to a load.dat file. But while writing to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.