Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / int to char

Author
Message
gdogg2k
19
Years of Service
User Offline
Joined: 20th May 2004
Location: Cornwall, UK
Posted: 9th Dec 2004 05:06
is there a quick way to use a int as the third param of dbText???

If i dont know,
I'm sure you will
OSX Using Happy Dude
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 9th Dec 2004 05:43
The string part ?

You can try something like :

x=4096;
dbText(0,0,(char *) x);

Becareful as invalid values will produce a crash.

Walk softly... and carry a big gun...
roujesky
19
Years of Service
User Offline
Joined: 4th Jun 2004
Location:
Posted: 9th Dec 2004 06:03
The above will print what ever is at address 4096.
I may be missing the point, but shouldn't it be more like:
x=4096;
char str[20];
itoa(x, str);
dbText(0,0,str);

This way prints out "4096".

Also, there appears to be dbStr$ which does the same thing simpler, but I have not used it....
gdogg2k
19
Years of Service
User Offline
Joined: 20th May 2004
Location: Cornwall, UK
Posted: 9th Dec 2004 15:36 Edited at: 9th Dec 2004 15:38
help file lists it as char* dbStr$ ( char* szString, int iValue )
where reallly it is char* dbStr( int IValue )

think they must have copied straight from dbpro help

If i dont know,
I'm sure you will
OSX Using Happy Dude
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 9th Dec 2004 17:06
Quote: "I may be missing the point, but shouldn't it be more like:
"

Possible, but it was a bit vague...

It could also be done like :

sprintf(t,"%d",x);
dbText(0,0,t);

Walk softly... and carry a big gun...
gdogg2k
19
Years of Service
User Offline
Joined: 20th May 2004
Location: Cornwall, UK
Posted: 9th Dec 2004 17:23
got it working dbStr

If i dont know,
I'm sure you will
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 9th Dec 2004 20:06
The only problem now, is to decide whether the memory returned should be deallocated by you, or whether it should be left for deallocation by the engine.

If you delete it, but the engine tries to delete it later - corruption
If you don't delete it, and neither does the engine - memory leak

I'll post some code later this evening which will let you treat dbText in the same way that printf does - I wrote it for exactly this purpose.

*** Coming soon - Network Plug-in - Check my site for info ***
For free Plug-ins and source code http://www.matrix1.demon.co.uk
IanG
19
Years of Service
User Offline
Joined: 25th Sep 2004
Location: Cyberspace
Posted: 10th Dec 2004 00:27
could you just do

x=4096;
dbText(0,0,(char) x);

Used to be Phoenix_insane registered in september 2003 despite what the date says to the left <--
PC - amd athlon 2.0ghz, 512mb, GeForce FX 5200 128mb, 200gb, xp pro sp2
OSX Using Happy Dude
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 10th Dec 2004 00:42
Nope - you would then get 1 character - either the ASCI code for $0, or the code for $10 - neither of which are printable anyway.

Walk softly... and carry a big gun...
gdogg2k
19
Years of Service
User Offline
Joined: 20th May 2004
Location: Cornwall, UK
Posted: 10th Dec 2004 01:20
i used dbText(0,0,(char*)x);
which displayed blankness
so then i tried - char* tmp = ((char*)x);
then used dbText to print tmp which works ok, but i dont do n e dealloc myself and every now and again get an error.
So the code would be great IanM

If i dont know,
I'm sure you will
IanG
19
Years of Service
User Offline
Joined: 25th Sep 2004
Location: Cyberspace
Posted: 10th Dec 2004 01:28
could you do this then

x=4096;
dbText(0,0,(string) x);

??

Used to be Phoenix_insane registered in september 2003 despite what the date says to the left <--
PC - amd athlon 2.0ghz, 512mb, GeForce FX 5200 128mb, 200gb, xp pro sp2
OSX Using Happy Dude
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 10th Dec 2004 01:30
Before we go any further - what do you want printed ? The value of the integer x or the contents of x ?

Walk softly... and carry a big gun...
gdogg2k
19
Years of Service
User Offline
Joined: 20th May 2004
Location: Cornwall, UK
Posted: 10th Dec 2004 01:54
the value of the integer, but dbText only takes char* and i wanted to use dbText so i can specify the position to print

If i dont know,
I'm sure you will
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 10th Dec 2004 03:00
Ok, I've zipped up the code and attached it - there are just two files.

You just use it like you would use printf.



*** Coming soon - Network Plug-in - Check my site for info ***
For free Plug-ins and source code http://www.matrix1.demon.co.uk

Attachments

Login to view attachments
billy the kid
19
Years of Service
User Offline
Joined: 7th Dec 2004
Location:
Posted: 10th Dec 2004 03:01
Ok this is the exact code for converting an int to a string in c++, then using dbText():
billy the kid
19
Years of Service
User Offline
Joined: 7th Dec 2004
Location:
Posted: 10th Dec 2004 07:08
For some reason I cant edit my post, so Ill make a new post instead.

I see he beat me to it. And my code didnt get attached properly, this is what I had:



IanM's solution is more elegant for using dbText(). However my method will work whenever you want to convert an int to a string for any purpose. Also you can do a similar thing for doubles, floats, etc.
The Wendigo
21
Years of Service
User Offline
Joined: 13th Sep 2002
Location: A hole near the base of a tree in the US
Posted: 10th Dec 2004 09:31
I would throw Billy's into a function like so:



then you could just do this:


Mind you, C++ isn't my native coding language, and I haven't tested this (nor do I even currently own the SDK), but this is the basic idea.


Home of DOOP, Strata Works, and Height
billy the kid
19
Years of Service
User Offline
Joined: 7th Dec 2004
Location:
Posted: 10th Dec 2004 11:24
Yeah good point, throw it into a function.

And that should work. Though there is no need for the const or static, but it doesnt hurt either.
OSX Using Happy Dude
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 10th Dec 2004 17:38
Where do you get TextSupport.h from ?

Walk softly... and carry a big gun...
IanM
Retired Moderator
21
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 10th Dec 2004 20:30
The TextSupport.h is in the zip file - it's the header file for the TextSupport.cpp.

Quote: "Though there is no need for the const or static, but it doesnt hurt either"


Oh yes there is. Without the 'static' the storage space is lost when the function returns as it is stored on the stack.

The const has no meaning where it is, but the return value shoud be 'const char*'. Without this 'const' people would be able to change the contents, and possibly overwrite other variables (if static) or the stack (if not static) if they were not careful.

In addition, the storage space should be 2 characters larger, as an int can have up to 10 characters, plus a sign (if negative) and a terminating null.


The other problem with this method is that you cannot use it twice on the same line, and it can be the cause of subtle bugs (contrived example to explain my meaning):



The result will be (depending on the compiler, or even options within the compiler)

First: 123, Second: 123

or

First: 456, Second: 456

You will almost definitely get even worse problems if you ever use the routine within two different threads.

*** Coming soon - Network Plug-in - Check my site for info ***
For free Plug-ins and source code http://www.matrix1.demon.co.uk
OSX Using Happy Dude
20
Years of Service
User Offline
Joined: 21st Aug 2003
Location: At home
Posted: 10th Dec 2004 20:33
Ah - one of your things...

Walk softly... and carry a big gun...
billy the kid
19
Years of Service
User Offline
Joined: 7th Dec 2004
Location:
Posted: 10th Dec 2004 23:11
What compiler do you use? I use VC++ 6 and VC++ .NET. I have never had any of the above problems, and I have used a function similar to that before many times. Although I never did any multi-threading with it. Maybe with a more strict ANSI compiler it matters. And the 10 was really just for example, guess I should have put 50 which is usually my default string length value to avoid ridicule.
billy the kid
19
Years of Service
User Offline
Joined: 7th Dec 2004
Location:
Posted: 11th Dec 2004 00:16
Ok ignore my previous post. I posted before I tried this out, I always do that. Anyway...

Ok for me the const stuff doesnt matter. It still is broken with or without const int and/or const char*. Doesnt mean you arent right, just didnt seem to matter for this particular case. However I do get that strange behaviour with calling the function twice on the same line. I found a similar problem too. If I do this:

printf("%s %s\n", stringVal(4096), stringVal(123));

I get "4096 4096" in the console. If I do this:

char* str = stringVal(4096);
char* str2 = stringVal(123);
printf("%s %s\n", str, str2);

I get "123 123" in the console.

Also I tried your string length thing. As long as the length of the string is >= to the number of actual characters in the number, its fine for me. Your argument does make sense from a memory standpoint. But what sprintf seems to do is convert the number directly into a string of chars. So the char '4' is really size 1, not size 4 like the int 4 is. Make sense?

I didnt try any multi-threading.

Still I find it strange that stuff happens. To me its almost a bug. The stack explanation does make sense, just seems weird I cant do it. Learn a new thing everyday.

Any ideas how to make it work no matter what? BTW I mean completely within the function.

Login to post a reply

Server time is: 2024-04-25 02:59:54
Your offset time is: 2024-04-25 02:59:54