Solved

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

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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 video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

724 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