Solved

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

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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