?
Solved

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

Posted on 2004-08-31
3
Medium Priority
?
2,614 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Accepted Solution

by:
chrisevans123 earned 225 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 225 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 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