Converting an int to a string in C on IRIX. -
KayBee - 01-19-2021
Hi Again,
Stack Overflow has so much on this for newer OSes, any of you smart people know an IRIX C straightforward method to convert integers to chars? I want to be able to print my numeric variables to the screen in IRISGL, which offers charstr(); to put characters on-screen with graphics.
I'd appreciate some advice, best would be an example.
Thank you.
KB
RE: Converting an int to a string in C on IRIX. -
weblacky - 01-19-2021
I'm having difficulty with this question, are you asking?
1. How do I get the integer representation of a ASCII character in C? I want the value of like 't'?
Or
2. How do I place a rendered character (or string of characters) on an OpenGL window?
Because these are completely different. If you're asking #2, unless you use a helper library, you're expected to RENDER everything...including typefaces/character set in OpenGL. Like literally draw characters...not say "print hello world" on an OpenGL window. There isn't any "text" in the OpenGL standard. So if you're asking about printing a text output to an OpenGL window. You'll likely need to find a library to literally draw the character instructions for you, unless you're very interesting in how to render fonts in OpenGL.
I looked into this decades ago, I don't know about Vulkan (which isn't in Irix), but I'm fairly sure from at least OpenGL 2.0 and lower, my statement is factually correct.
If I'm off base on my assumption, I apologize for the confusion.
RE: Converting an int to a string in C on IRIX. -
robespierre - 01-19-2021
(01-19-2021, 05:06 AM)KayBee Wrote: an IRIX C straightforward method to convert integers to chars?
I want to be able to print my numeric variables to the screen in IRISGL, which offers charstr(); to put characters on-screen with graphics.
The traditional way is to use sprintf().
You should keep in mind that sprintf() has the possibility of a buffer overflow if you use a buffer that is shorter than the maximum possible output (which depends on the possible inputs, and this is related to network security and input validation issues). But for inputs that are entirely generated by your code it can be used safely.
No time for a worked example now, sorry.
RE: Converting an int to a string in C on IRIX. -
Raion - 01-19-2021
libxg has asprintf and vasprintf if that helps.
RE: Converting an int to a string in C on IRIX. -
KayBee - 01-24-2021
Hello Again all, closing this question, thanks to the feedback and some digging I have gl commands and text coexisting now. If you are curious to try it, here is an example:
Code:
// compile with cc example.c example -lc -lfm -lgl -lm
#include <gl/gl.h>
#include <gl/device.h>
#include <fmclient.h>
#include <stdio.h>
#include <stdlib.h>
main( ) {
short val;
char char_arr [100];
int num = 7;
fmfonthandle font1, font25;
prefsize(240,210);
winopen("Hello");
gconfig();
color(BLACK);
clear( );
color(GREEN);
fminit( );
/* Exit if cant find the font family */
if ((font1=fmfindfont("Helvetica")) == 0) exit (1);
/* scale the 1-point-high font to 25 points */
font25 = fmscalefont(font1, 25.0);
fmsetfont(font25);
cmov2(30, 100);
sprintf(char_arr, "%d", num);
fmprstr(char_arr);
color (RED);
move2s(0,0); draw2s(240,210);
sleep(10);
}
Thanks again.
KB
RE: Converting an int to a string in C on IRIX. -
jan-jaap - 01-24-2021
A 32bit integer has at most 10 digits, a 64bit integer (was IRISGL ever 64bit?) 20 digits, so a 100char array is wasteful.
If you're afraid of overrunning the array, use snprintf().
RE: Converting an int to a string in C on IRIX. -
robespierre - 01-25-2021
(01-24-2021, 09:17 PM)jan-jaap Wrote: A 32bit integer has at most 10 digits, a 64bit integer (was IRISGL ever 64bit?) 20 digits, so a 100char array is wasteful.
Note that you must provide enough space for the output string plus a \0. And the output may have a sign with the default options. So sizeof 12 should be enough...if locale issues don't crop up.
Quote:If you're afraid of overrunning the array, use snprintf().
I wasn't positive that snprintf() existed in the system being used. It isn't included in C89.
Raion Wrote: libxg has asprintf and vasprintf if that helps.
snprintf() if it is available is good. No need to heap allocate memory for this.