473,396 Members | 2,147 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,396 software developers and data experts.

char[] to byte[]


I have this case:

int bufferLen = Convert.ToInt32(file.Length);
char[] buffer = new char[bufferLen];
int len = sr.Read(buffer, 0, bufferLen);
rawBytes.setBytesArray(buffer);

While executing above code I got this (COM, automation) error at last line:

An unhandled exception of type
'System.Runtime.InteropServices.SafeArrayTypeMisma tchException' occurred in
MyApp.exe

That is because buffer have to be of byte[] type and not char[]. But, if I
change "buffer" to byte[] than I got compiler error saying that sr.Read() is
expecting char[] instead of byte[]. :(

What should I do? Cast? How? Something like this:

int len = sr.Read((char[])buffer, 0, bufferLen);
Nov 17 '05 #1
3 6632
If you need to do some quick and easy conversion of arrays between char[] and
byte[], take a look at the GetBytes() and GetChars() methods contained within
System.Text.Encoding.<Desired Encoding>.

Brendan
"Petar Popara" wrote:

I have this case:

int bufferLen = Convert.ToInt32(file.Length);
char[] buffer = new char[bufferLen];
int len = sr.Read(buffer, 0, bufferLen);
rawBytes.setBytesArray(buffer);

While executing above code I got this (COM, automation) error at last line:

An unhandled exception of type
'System.Runtime.InteropServices.SafeArrayTypeMisma tchException' occurred in
MyApp.exe

That is because buffer have to be of byte[] type and not char[]. But, if I
change "buffer" to byte[] than I got compiler error saying that sr.Read() is
expecting char[] instead of byte[]. :(

What should I do? Cast? How? Something like this:

int len = sr.Read((char[])buffer, 0, bufferLen);

Nov 17 '05 #2
Petar,
In addition to System.Text.Encoding, you might be able to use
System.Buffer.BlockCopy if you don't want any encoding changes at all.

Also instead of using a StreamReader, which is for Text encoded files, you
should consider simply use Stream.

Something like:

Stream stream = New FileStream(...);

| int bufferLen = Convert.ToInt32(file.Length);
| byte[] buffer = new byte[bufferLen];
int len = stream.Read(buffer, 0, bufferLen);
| rawBytes.setBytesArray(buffer);

Hope this helps
Jay

"Petar Popara" <my*****@mail.net> wrote in message
news:O1**************@TK2MSFTNGP12.phx.gbl...
|
| I have this case:
|
| int bufferLen = Convert.ToInt32(file.Length);
| char[] buffer = new char[bufferLen];
| int len = sr.Read(buffer, 0, bufferLen);
| rawBytes.setBytesArray(buffer);
|
| While executing above code I got this (COM, automation) error at last
line:
|
| An unhandled exception of type
| 'System.Runtime.InteropServices.SafeArrayTypeMisma tchException' occurred
in
| MyApp.exe
|
| That is because buffer have to be of byte[] type and not char[]. But, if I
| change "buffer" to byte[] than I got compiler error saying that sr.Read()
is
| expecting char[] instead of byte[]. :(
|
| What should I do? Cast? How? Something like this:
|
| int len = sr.Read((char[])buffer, 0, bufferLen);
|
|
Nov 17 '05 #3
Stream stream = New FileStream(...);


Thank you. :)
Nov 17 '05 #4

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

Similar topics

2
by: Nicholas Parnell | last post by:
Hi! I have the problem where I cast a bit string of "10000000" to a byte, which i get -128; this is fine so far. However, when I take this byte and cast it to a char, I get a question mark('?')....
11
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I've noticed a few threads (full of sound and fury, signifying nothing) here recently about allocation of large memory blocks. I'm about to start on a personal pet project where I'll be using...
15
by: T Koster | last post by:
Hi group, I'm having some difficulty figuring out the most portable way to read 24 bits from a file. This is related to a Base-64 encoding. The file is opened in binary mode, and I'm using...
11
by: QQ | last post by:
I know a char is 2 bytes, the conversion is like byte byte_array = new byte; //Allocate double mem as that of char then for each char do byte = (byte) char & 0xff byte = (byte)( char >> 8 &...
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
5
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array...
20
by: =?Utf-8?B?ZW1pdG9qbGV5ZXM=?= | last post by:
Hi everyone: i read from the documentation of a dll that there is a struct that uses char for storing an IP address. How can it be? and how come i can get to representate the same value in a...
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
5
by: Neel | last post by:
Hi friends, How can I store a short into an unsigned char??? its like unsigned char msg; //now I want to assign 0xAB into it.
3
by: diadomraz | last post by:
Hi, This is a question about declaring a signed 8bit numeric type in C++ that prints like a numeric variable and not like a char variable. I would like to declare a type with name 'byte' which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.