473,804 Members | 3,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(File Name, FileMode::Open,
FileAccess::Rea d);

__int64 myFileSize = myFile->get_Length() ;

Byte[] myFileData;
myFile1Data = new Byte[myFileSize];
myFile->Read(myFile1Da ta, 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 2342
Howard Weiss wrote:
I am reading a file (containing short integers). To read the file, I use
the following

FileStream *myFile = new FileStream(File Name, FileMode::Open,
FileAccess::Rea d);

__int64 myFileSize = myFile->get_Length() ;

Byte[] myFileData;
myFile1Data = new Byte[myFileSize];
myFile->Read(myFile1Da ta, 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(Syst em::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(File Name, FileMode::Open,
FileAccess::Rea d);

__int64 myFileSize = myFile->get_Length() ;

Byte[] myFileData;
myFile1Data = new Byte[myFileSize];
myFile->Read(myFile1Da ta, 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
4161
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
14
29530
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 a = (bool)i1; // ERROR : convert type 'byte' to 'bool'
6
10237
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 the data converted from the byte array into a string , is not what i expect. example: - the data returned from the socket is (byte array) "Usuario Sin Atribuciones Para Esta Función"
15
34614
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
5914
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 vFileHandle is supposed to be a file handle (an IntPtr, I suppose). How can I convert this Byte() in this IntPtr ?
5
8984
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 accessing some specialized image types. It seems I am returned an array of object's (declared as
4
5327
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 byteArray); However, this results in following error: Cannot convert 'ref byte' to 'ref byte'
0
9582
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9157
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7621
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3821
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.