Converting string to/from integer

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Converting String Into int Using atoi

Before I leave the string section, I'd like to talk about two useful functions that could come in handy later on. Both of these require the stdlib.h header file.

First of all, atoi. This converts strings, like "23" or even "29dhjds" into integers (returning 23 and 29 respectively in this case).

atoi requires one char * argument and returns an int (not a float!).

If the string is empty, or first character isn't a number or a minus sign, then atoi returns 0.

If atoi encounters a non-number character, it returns the number formed up until that point.

#include
#include
int main() {
char str1[] = "124z3yu87";
char str2[] = "-3.4";
char *str3 = "e24";
printf("str1: %d/n", atoi(str1));
printf("str2: %d/n", atoi(str2));
printf("str3: %d/n", atoi(str3));
return 0;
}

Output:

str1: 124
str2: -3
str3: 0

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Converting int Into String Using itoa (NOT ANSI C)

Before I continue, I must warn you that itoa is NOT an ANSI function, (it's not a standard C function). You should use sprintf to convert an int into a string, which I'll show you in a moment.

I'll cover itoa in case you've ever wondered what it does.

itoa takes three arguments.

The first one is the integer to be converted.

The second is a char * variable - this is where the string is going to be stored. My program crashed if I pass in a char * variable I've already used, so I passed in a normal sized char array and it worked fine.

The last one is NOT the size of the array, but it's the BASE of your number - base 10 is the one you're most likely to use. Base 2 is binary, 8 is octal and 16 is hexadecimal.

For a small lesson on bases, go back to the Hexadecimal section.

itoa is a very useful function, which is supported by some compilers - shame it isn't support by all, unlike atoi.

Maybe converting a base 10 number into binary format as a string isn't so hard after all...

#include
#include

int main() {
char str[10]; /* MUST be big enough to hold all the characters of your number!! */

printf("15 in binary is %s/n", itoa(15, str, 2));
printf("15 in octal is %s/n", itoa(15, str, 8));
printf("15 in decimal is %s/n", itoa(15, str, 10));
printf("15 in hex is %s/n", itoa(15, str, 16));
return 0;
}

Output:
15 in binary is 1111
15 in octal is 17
15 in decimal is 15
15 in hex is f

... itoa can be useful when you know your program doesn't have to be ANSI C only.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sprintf

You can use this function as an alternative to itoa. It's only half as good as itoa because you can't specify the base of your number to be converted.

sprintf takes three arguments.

The first has to be a char * variable, which means you can use a char array, but make sure it's big enough to hold the converted number.

The second argument is a string containing a format specifier, depending on the format of the number you want to convert.

The third argument is the number you want to convert into a string.

sprintf returns the number of characters in the string (not included the null character).

This example convert a few numbers into string format, and prints out the result:

#include

int main() {
char str[10]; /* MUST be big enough to hold all the characters of your number!! */
int i;

i = sprintf(str, "%o", 15);
printf("15 in octal is %s/n", str);
printf("sprintf returns: %d/n/n", i);

i = sprintf(str, "%d", 15);
printf("15 in decimal is %s/n", str);
printf("sprintf returns: %d/n/n", i);

i = sprintf(str, "%x", 15);
printf("15 in hex is %s/n", str);
printf("sprintf returns: %d/n/n", i);

i = sprintf(str, "%f", 15.05);
printf("15.05 as a string is %s/n", str);
printf("sprintf returns: %d/n/n", i);

return 0;
}

Output:
15 in octal is 17
sprintf returns: 2

15 in decimal is 15
sprintf returns: 2

15 in hex is f
sprintf returns: 1

15.05 as a string is 15.050000
sprintf returns: 9

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sscanf

For completeness, I'm going to cover sscanf, seeing that it's paired with sprintf.

You could've guessed that it converts a string into various formats.

sscanf takes three arguments, for example:

sscanf(str, "%d", &num);

The first is a char * variable that contains data to be converted.

The second is a string containing a format specifier that determines how the string is converted.

The third is a memory location to place the result of the conversion. Most of the time, you'll need the "address of" operator (&), then a variable name, or you can place a char * variable here.

Now, if the string you pass into sscanf contains a space, only the data up until that space is converted.

sscanf returns the number of items converted.

This example performs conversions in several formats:

#include

int main() {
char* ints = "20, 40, 60";
char* floats = "10.4, 24.66";
char* hex = "FF, F";

int i;
int n;
float f;
int h;
char* s;

i = sscanf(ints, "%d", &n);
printf("n: %d/n", n);
printf("sscanff returns: %d/n/n", i);

i = sscanf(floats, "%f", &f);
printf("f: %f/n", f);
printf("sscanff returns: %d/n/n", i);

i = sscanf(hex, "%x", &h);
printf("h: %d/n", h);
printf("sscanff returns: %d/n/n", i);

i = sscanf(ints, "%s", s);
printf("s: %s/n", s);
printf("sscanff returns: %d/n/n", i);

return 0;
}

Output:
n: 20
sscanff returns: 1

f: 10.400000
sscanff returns: 1

h: 255
sscanff returns: 1

s: 20,
sscanff returns: 1

Notice how sscanf ignores the comma when converting to a number (see the result for the string variable, s).
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值