473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Calling C++ Functions

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.
Nov 17 '05 #1
4 3518
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" <s.***********@nospam.btinternet.com> wrote in message
news:uk****************@TK2MSFTNGP12.phx.gbl...
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.

Nov 17 '05 #2
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" <sj*@fixerlabs.com.NOSPAM> wrote in message
news:#2**************@tk2msftngp13.phx.gbl...
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" <s.***********@nospam.btinternet.com> wrote in message news:uk****************@TK2MSFTNGP12.phx.gbl...
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.


Nov 17 '05 #3

"Steven Livingstone" <s.***********@nospam.btinternet.com> wrote in message
news:uk****************@TK2MSFTNGP12.phx.gbl...
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/de..._functions.asp )

Nov 17 '05 #4
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" <s.***********@nospam.btinternet.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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" <sj*@fixerlabs.com.NOSPAM> wrote in message
news:#2**************@tk2msftngp13.phx.gbl...
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" <s.***********@nospam.btinternet.com> wrote in message
news:uk****************@TK2MSFTNGP12.phx.gbl...
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.



Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: pieter.breed | last post by:
Hi All, Is it possible to export a c# method into a dll in such a way that your "normal" C application can then call this method? To be clear: I am not asking how to use "DllImport" or...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
5
by: Dave | last post by:
does calling a regular function cost any cpu time? In other words, is it faster to write the code of two functions into main(), or is it the exact same thing as calling two functions. I know its...
1
by: Mark Jerde | last post by:
Yesterday I posted the message below to microsoft.public.dotnet.languages.vb and microsoft.public.vc.language. The two replies are also posted. I need to write some ISO C++ functions, more...
1
by: Jesse McGrew | last post by:
Hi all, I'm trying to make a plugin DLL for a third-party application, using VC++ .NET 2003. This DLL acts as a bridge between the C++ plugin API of the application, and my actual plugin code...
1
by: H.B. | last post by:
Hi, I need to make a function that can display data on my Managed C++ app and be called by an unmanaged C++ DLL. Something like : void Form1::Form1_Load(System::Object * sender,...
2
by: Daniel Lidström | last post by:
I'm using a library called fyba. This library reads and writes files in a format called sosi. fyba uses the following code to determine if the calling process has own methods to handle errors,...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
4
by: Edwin Gomez | last post by:
I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is that I dispatch a thread and when the thread...
10
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.