Solved

Converting from "C# byte[]" to "C++ unsigned char *"

Posted on 2004-08-31
3
2,354 Views
Last Modified: 2008-01-09
I have a C# method which has image data in a byte array

byte [] myImage


I want to call a C++ DLL that receives the image data as an unsigned char *.

void myDLL (unsigned char *buf)

Can anyone tell me how to convert the byte [] to an unsigned char *?

0
Comment
Question by:wile_e_coyote
3 Comments
 
LVL 3

Accepted Solution

by:
chrisevans123 earned 75 total points
ID: 11946590
I may be missing something but i think you can just cast them i.e.

public char[] toChars(byte[] bytes) {
char[] chars = new char[bytes.Length];
for (int counter = 0; counter < bytes.Length; counter++) {
char[counter] = (char) bytes[counter];
}
return chars;
}

then import your dll and call the function with ref.

this.callCppFunction(ref chars);
0
 
LVL 7

Assisted Solution

by:NipNFriar_Tuck
NipNFriar_Tuck earned 75 total points
ID: 11953169
Here is a method I wrote to convert bytes to string...

/// <summary>
/// Convert a byte array to a string.
/// </summary>
/// <param name="bytes">byte array to convert to a string</param>
/// <returns>byte array</returns>
public static string Bytes2Str(byte[] bytes)
{
    char[] chars = Encoding.Default.GetChars(bytes, 0, bytes.Length);
    return new string(chars);
}

The first line in the method will convert an array of bytes to an array of chars.
0
 
LVL 4

Author Comment

by:wile_e_coyote
ID: 11953211
Gentlemen - thanks for your help.  I'm going to bump this question up to 150 points and give each of you 75 points.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now