Converting an int to a string in C on IRIX.
#1
Converting an int to a string in C on IRIX.
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
(This post was last modified: 01-19-2021, 05:45 AM by KayBee.)
KayBee
Octane

Trade Count: (0)
Posts: 132
Threads: 40
Joined: Feb 2020
Find Reply
01-19-2021, 05:06 AM
#2
RE: Converting an int to a string in C on IRIX.
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.
weblacky
I play an SGI Doctor, on daytime TV.

Trade Count: (10)
Posts: 1,716
Threads: 88
Joined: Jan 2019
Location: Seattle, WA
Find Reply
01-19-2021, 06:24 AM
#3
RE: Converting an int to a string in C on IRIX.
(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.

Personaliris O2 Indigo2 R10000/IMPACT Indigo2 R10000/IMPACT Indigo2 Indy   (past: 4D70GT)
robespierre
refector peritus

Trade Count: (0)
Posts: 640
Threads: 3
Joined: Nov 2020
Location: Massholium
Find Reply
01-19-2021, 06:41 AM
#4
RE: Converting an int to a string in C on IRIX.
libxg has asprintf and vasprintf if that helps.

I'm the system admin of this site. Private security technician, licensed locksmith, hack of a c developer and vintage computer enthusiast. 

https://contrib.irixnet.org/raion/ -- contributions and pieces that I'm working on currently. 

https://codeberg.org/SolusRaion -- Code repos I control

Technical problems should be sent my way.
Raion
Chief IRIX Officer

Trade Count: (9)
Posts: 4,240
Threads: 533
Joined: Nov 2017
Location: Eastern Virginia
Website Find Reply
01-19-2021, 07:07 AM
#5
RE: Converting an int to a string in C on IRIX.
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
KayBee
Octane

Trade Count: (0)
Posts: 132
Threads: 40
Joined: Feb 2020
Find Reply
01-24-2021, 07:03 PM
#6
RE: Converting an int to a string in C on IRIX.
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().
jan-jaap
SGI Collector

Trade Count: (0)
Posts: 1,048
Threads: 37
Joined: Jun 2018
Location: Netherlands
Website Find Reply
01-24-2021, 09:17 PM
#7
RE: Converting an int to a string in C on IRIX.
(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.

Personaliris O2 Indigo2 R10000/IMPACT Indigo2 R10000/IMPACT Indigo2 Indy   (past: 4D70GT)
(This post was last modified: 01-25-2021, 07:56 AM by robespierre.)
robespierre
refector peritus

Trade Count: (0)
Posts: 640
Threads: 3
Joined: Nov 2020
Location: Massholium
Find Reply
01-25-2021, 04:15 AM


Forum Jump:


Users browsing this thread: 1 Guest(s)