473,386 Members | 1,752 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,386 software developers and data experts.

How to convert form Byte pointer to Int16 pointer

I am reading a file (containing short integers). To read the file, I use
the following

FileStream *myFile = new FileStream(FileName, FileMode::Open,
FileAccess::Read);

__int64 myFileSize = myFile->get_Length();

Byte[] myFileData;
myFile1Data = new Byte[myFileSize];
myFile->Read(myFile1Data, 0, myFileSize);

This reads the contents of the file into memory. I would now like to access
the data as an Array of UInt16.

However, Managed C++ does not allow me to perform a simple cast, e.g.

UInt16 *myUInt16 = (UInt16 *) myFileData;

Can I do what I want? If not, why not?

Howard Weiss
Nov 17 '05 #1
2 2300
Howard Weiss wrote:
I am reading a file (containing short integers). To read the file, I use
the following

FileStream *myFile = new FileStream(FileName, FileMode::Open,
FileAccess::Read);

__int64 myFileSize = myFile->get_Length();

Byte[] myFileData;
myFile1Data = new Byte[myFileSize];
myFile->Read(myFile1Data, 0, myFileSize);

This reads the contents of the file into memory. I would now like to access
the data as an Array of UInt16.

However, Managed C++ does not allow me to perform a simple cast, e.g.

UInt16 *myUInt16 = (UInt16 *) myFileData;

Can I do what I want? If not, why not?


You can't just convert managed pointers to unmanaged pointers.

I believe the most simple and fastest method is to create a managed
array, pin it and then use memcpy to copy the data from the buffer to
the managed array:

I use code similar to this in a project of mine:
array<Byte>^ test = gcnew array<Byte>(400);
pin_ptr<Byte> ptrTest = &test[0];
memcpy(ptrTest, _pFileBase, 400);
ptrTest = nullptr;

I'm sorry about the C++/CLI syntax. In MC++ it should be something like
this (untested):

Byte* test[] = new __gc Byte[400]; //??
int __pin* pTest = &test[0];
memcpy(pTest, _pFileBase, 400);
pTest = 0;
If there is a better way to do this, I'd be interested in it as well

Ben
Nov 17 '05 #2
Hi,

Following is a Sample of how to manipulate a GC Byte array in unmanaged C++ ( as an 'unsigned short' array ) without copying the content of the array:

virtual int ManipArray(System::Byte btArray __gc[])
{
int i = 0;
BYTE __pin *pbtArray = &btArray[0];
unsigned short *uiArray = (unsigned short*)pbtArray;
for(i; i < btArray->Length/2; i++)
uiArray[i] <<= 1;
return 0;
}

Hope this help...

Nadav
http://www.ddevel.com
"Howard Weiss" wrote:
I am reading a file (containing short integers). To read the file, I use
the following

FileStream *myFile = new FileStream(FileName, FileMode::Open,
FileAccess::Read);

__int64 myFileSize = myFile->get_Length();

Byte[] myFileData;
myFile1Data = new Byte[myFileSize];
myFile->Read(myFile1Data, 0, myFileSize);

This reads the contents of the file into memory. I would now like to access
the data as an Array of UInt16.

However, Managed C++ does not allow me to perform a simple cast, e.g.

UInt16 *myUInt16 = (UInt16 *) myFileData;

Can I do what I want? If not, why not?

Howard Weiss

Nov 17 '05 #3

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

Similar topics

19
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
14
by: Chris | last post by:
Hi, I try to print out truth-tables for an &&-operation using the following code, unfortunatly I get compiler errors : for ( byte i1=0; i1<=1; i1++) { for ( byte i2=0; i2<=1; i2++) { bool...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
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?
8
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The...
5
by: Lint Radley | last post by:
Hello, I am looking for a way to cast a byte to Int16. I get this error: "Unable to cast object of type 'System.Byte' to type 'System.Int16'" If you're curious, I am using a library for...
4
by: Pramod | last post by:
Hi. I have a C++ method which accepts - byte* - and I am calling this method from a C# application. I have created a byte to pass by ref. This is how I call the C++ method: Method(ref...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.