Jump to content

Photo

itos()


  • Please log in to reply
8 replies to this topic

#1 nyrox

nyrox

    Junior

  • Members
  • Location:Québec

Posted 25 June 2020 - 11:13 PM

i want use correctly  string fonction

is for change  a integer at string

 

int Tr_Z[6] =  itos(1)+  "/" +  itos(2);
 
the resul I want is in Tr_Z was:  1/2  in string 
 
is for make a Screen->DrawString()

Edited by nyrox, 25 June 2020 - 11:15 PM.


#2 Mani Kanina

Mani Kanina

    Rabbits!

  • Members

Posted 26 June 2020 - 01:37 AM

AFAIK this just works:
 

int Variable1 = 10;
int Variable2 = 50;

int StringName[] = Variable1 + "/" + Variable2;

So I'm not entirely sure what your issue is?

I don't think the itos function is something you want to use in a scenario like this (also, isn't it itoa?).


Conversely, if you just want the string to always read "1/2" then...
 

int StringName[] = "1/2";

Edited by Mani Kanina, 26 June 2020 - 04:22 AM.


#3 nyrox

nyrox

    Junior

  • Members
  • Location:Québec

Posted 26 June 2020 - 06:35 AM

I tried like you just put the

int x = 16;

int y = 48;

int Tr_Z [] = x + "/" + y;

and the following error message is:

unexpected identifier , expecting lbrace or quotedstring on token x

 

my real line is:

int Tr_Z[] =  ZoneTresorPickup[ListZoneLevel[i]] + "/" +  ZoneTresorMax[ListZoneLevel[i]];

and is the same error

 

is it for tu put in screen the tresor pickup/ max tresor  for one zone in my game


Edited by nyrox, 26 June 2020 - 06:51 AM.


#4 Mani Kanina

Mani Kanina

    Rabbits!

  • Members

Posted 26 June 2020 - 01:53 PM

Oh, hmm, I guess that does not exist in earlier versions of ZQ. I'd imagine you could still do it, but hell if I have a clue how and let's be honest, the documentation is a bit lacking.
(I'm fairly confident I figured out how to do this for some quest project at one point, but I couldn't find it in any of the script files I went through) 

As always, there is always the alternate jank solution instead:
Just handle everything in three draw commands instead of two.
Screen->DrawInteger for the first var
Screen->DrawString for the / symbol
Screen->DrawInteger for the second var

Edited by Mani Kanina, 26 June 2020 - 01:54 PM.


#5 Emily

Emily

    Scripter / Dev

  • ZC Developers

Posted 26 June 2020 - 02:04 PM

Err, ZScript can't do string concatenation with '+'.

int str[] = x + "/" + y;

^This doesn't work. That's not a thing.

What you want:

int x = 1;
int y = 2;
int str[64];
sprintf(str, "%d/%d", x, y);

'sprintf()' is used for printing a formatted string into a buffer. This is part of 'string_functions.zh' in 2.53, and it is an internal ZScript function in 2.55.

Docs (from 2.55):

void sprintf(char32[] buf, char32[] format, ...untyped...)
 * Takes a buffer, a format string, and anywhere between 0 and 16 (inclusive) untyped arguments.
 * The untyped arguments will be inserted to the string at points in the format string.
 * Points are specified using '%'. The letter after is used to determine the type of the arg.
 *     "%i" - Integer. Inserts the number as a string, without decimal places.
 *     "%f" - Float. Inserts the number as a string, with decimal places.
 *            If the number does not contain decimal places, ".0" will still appear.
 *     "%d" - Number value. Automatically chooses between "%i" and "%f".
 *     "%s" - String value. Parameter must be a valid ZScript string.
 *     "%p" - Pointer value. Identical to "%i".
 *     "%c" - Character value. Inserts a single character.
 *     "%x" - Hexadecimal, lowercase. Inserts the number, not including decimals,
 *            in hexidecimal. Uses lowercase for letters.
 *     "%X" - Hexadecimal, uppercase. Same as above, but uses uppercase for letters.
 *     "%%" - Inserts a single '%' character, without trying to read it as an argument.
 * The resulting string will be placed in the buffer, which is expected to be large enough
 *     to hold the output.

  • Mani Kanina likes this

#6 nyrox

nyrox

    Junior

  • Members
  • Location:Québec

Posted 02 July 2020 - 11:41 AM

I looked at the file 'string_functions.zh and I did not find a void sprintf() function but rather a int sprintf() and a bool sprintf(). i see a void printf() fonction.

 

Spoiler

 

 

i tryed your 'what you want" and the i have a error

 

UNEXPECTED QUOTEDSTRING, ON TOKEN %D/%D"

 

 

I'm have done with the drawInteger fonction Mani Kanina said before but  is not  the best methode

 

 

522_qcuvf5.png


Edited by nyrox, 02 July 2020 - 02:05 PM.


#7 Emily

Emily

    Scripter / Dev

  • ZC Developers

Posted 03 July 2020 - 09:13 PM

Oh, you can't just use "string" in 2.53. That's a 2.55 thing. You'd need to put it in an array first.

#8 Timelord

Timelord

    The Timelord

  • Banned
  • Location:Prydon Academy

Posted 23 July 2020 - 08:49 AM

Oh, you can't just use "string" in 2.53. That's a 2.55 thing. You'd need to put it in an array first.

 

I did not notice the error here, earlier:

 

sprintf, internally, does need to return int, for compatibility. In <std>, it acts as sprintf in C:

 

If successful, the total number of characters written is returned excluding the null-character appended at the end of the string, otherwise a negative number is returned in case of failure.

 

This is how the function has been defined for ages  (C, <std>).

 

printf, OTOH, has a void return type.



#9 Timelord

Timelord

    The Timelord

  • Banned
  • Location:Prydon Academy

Posted 23 July 2020 - 09:04 AM

 

i want use correctly  string fonction

is for change  a integer at string

 

int Tr_Z[6] =  itos(1)+  "/" +  itos(2);
 
the resul I want is in Tr_Z was:  1/2  in string 
 
is for make a Screen->DrawString()

 

 

itos() is not a valid function. You want itoa()., as this converts an integer to a string. This requires an intermediate buffer.

 

char32 buf[7]; //you can use ftoa() for float, with a size of [12]
itoa(buf, 1277);
//buf is now "1277"
 
char32 buf_f[12];
ftoa(buf_f. 1234.9876, true);
//puts "1234.9876" into buffer.
 
char32 buf_f2[12];
ftoa(buf_f2. 1, true);
//puts "1.0000" into buffer.
 
char32 buf_f3[12];
ftoa(buf_f3. 1, false);
//puts "1" into buffer.



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users