Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse an integer from a string in Zig?

What is the best way to parse an integer from a string in Zig and specify the resulting integer type?

const foo = "22";

How would I convert foo to an i32, for example?

like image 806
JPlusPlus Avatar asked Jun 16 '26 21:06

JPlusPlus


1 Answers

After looking through Zig's Standard Library Documentation, I've found std.fmt.parseInt, which allows you to parse a string to an integer of any size, signed or unsigned (e.g. i32, u64).

Example:

const foo = "22";

const integer = try std.fmt.parseInt(i32, foo, 10);
like image 76
JPlusPlus Avatar answered Jun 18 '26 10:06

JPlusPlus