Solved

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

Posted on 2004-08-31
3
2,442 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 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