Calling C++ Functions

  • Thread starter Thread starter Steven Livingstone
  • Start date Start date
S

Steven Livingstone

If I was using C# and wanted to call the printf function what would I do?

Specifically i want to convert a string to octal and as there is no facility
for doing this in C# i figured i'd look at using C++. Even better would be a
function that returns me a string or char array to C# when i call C++ rather
than printing it out.

I can get it working by calling VB from C#, but with C++ i am finding it a
little tricky!
Psuedocode below....

static void Main(string[] args)
{
char[] instr = Console.ReadLine().ToCharArray();
char[] outstr;


unsafe
{
outstr = ConvertToOctalHere(instr);
}

Console.WriteLine(outstr);

Console.ReadLine();
}

thanks,
Steven.
 
Hi,

Don't know whether or not there's a C# alternative, but it seems likely
there should be. In any case, sprintf (rather than printf) is probably the C
function to do what you want - it prints into a buffer you give it.

Steve
 
Steven Livingstone said:
If I was using C# and wanted to call the printf function what would I do?

Specifically i want to convert a string to octal and as there is no facility
for doing this in C# i figured i'd look at using C++. Even better would be a
function that returns me a string or char array to C# when i call C++ rather
than printing it out.

I can get it working by calling VB from C#, but with C++ i am finding it a
little tricky!
Psuedocode below....

static void Main(string[] args)
{
char[] instr = Console.ReadLine().ToCharArray();
char[] outstr;


unsafe
{
outstr = ConvertToOctalHere(instr);
}

Console.WriteLine(outstr);

Console.ReadLine();
}

thanks,
Steven.

Have a look at CString.Format, most likely CString.Format("%o", instr).
Possible values for the format are the same as printf (see
http://msdn.microsoft.com/library/d..._fields_.2d_.printf_and_wprintf_functions.asp )
 
Hi Steve - is it possible to call sprintf in an unsafe block in C#??

If so do you have an example? I looked all over the place, but my lack of
C++ knowledge if likely the cause of finding nothing that showd it actually
working!

Steve McLellan said:
Hi,

Don't know whether or not there's a C# alternative, but it seems likely
there should be. In any case, sprintf (rather than printf) is probably the C
function to do what you want - it prints into a buffer you give it.

Steve

If I was using C# and wanted to call the printf function what would I do?

Specifically i want to convert a string to octal and as there is no facility
for doing this in C# i figured i'd look at using C++. Even better would
be
a
function that returns me a string or char array to C# when i call C++ rather
than printing it out.

I can get it working by calling VB from C#, but with C++ i am finding it a
little tricky!
Psuedocode below....

static void Main(string[] args)
{
char[] instr = Console.ReadLine().ToCharArray();
char[] outstr;


unsafe
{
outstr = ConvertToOctalHere(instr);
}

Console.WriteLine(outstr);

Console.ReadLine();
}

thanks,
Steven.
 
Hi,

I don't currently use C#, so I'm not sure as to the 'safeness' of it. From
what little I know, unsafe blocks are required for anything using pointers,
which is what sprintf does, so it should be ok. From memory, the sprintf
syntax is 'sprintf ( char* buffer, const char* format ), so the following
code prints a couple of numbers as octals:

char buffer[50];
int a = 10, b = 5;
int usedBufferSize = ( buffer, "In octal, a = %o and b = %o", a, b );

usedBufferSize then contains the number of used chars in buffer.

Play around with it, you should be able to get it to work but as I say, I'm
not a C# programmer so there may be issues I'm unaware of.

HTH,

Steve

Steven Livingstone said:
Hi Steve - is it possible to call sprintf in an unsafe block in C#??

If so do you have an example? I looked all over the place, but my lack of
C++ knowledge if likely the cause of finding nothing that showd it actually
working!

Steve McLellan said:
Hi,

Don't know whether or not there's a C# alternative, but it seems likely
there should be. In any case, sprintf (rather than printf) is probably
the
C
function to do what you want - it prints into a buffer you give it.

Steve
would
it
a
little tricky!
Psuedocode below....

static void Main(string[] args)
{
char[] instr = Console.ReadLine().ToCharArray();
char[] outstr;


unsafe
{
outstr = ConvertToOctalHere(instr);
}

Console.WriteLine(outstr);

Console.ReadLine();
}

thanks,
Steven.
 
Back
Top