简体   繁体   中英

How to convert a char string into int string?

I am now writing a program that requires me to convert a string such as "1110011111100111" into an int string so that I can get access and do arithmetic operations on each index. Because what I need to do is to select a range of bits and calculate the decimal value. I can not do that without accessing the int value of the string. Probably storing them in an array would be a good option? Since I am fairly new to C, can anyone help me?

For example,
char* ch = "1110011111100111"; and the range is 0 and 3, I should return 7

If I understand your question correctly, your task is to write a function which takes as input a string and two int arguments representing a range of bits, and this function should return an int representing the value of this range, when interpreting the individual characters as bits.

If your string is not so long that the whole binary number won't fit into a long int (ie the whole number is not larger than LONG_MAX , which must be at least 4,294,967,295 , which corresponds to 32 bits), then you can use the function strtol to read in the whole number. You can then mask out all the bits that you are not interested in, by using the bitwise-AND operator & . Also, if the range does not start at 0 , then you should use the bit-shift operator >> to move all bits by that number.

On the other hand, maybe you are not supposed to use the function strtol to solve this problem. I cannot tell, because you did not mention any such restriction in the question, and in the comments section, you stated that you simply wanted the easiest solution. If you are not supposed to use this function, then you are probably supposed read the individual bits of the string in a loop, and use the loop to go through the desired range of the bits. For every bit you encounter that is set (ie equals 1 ), you can then add the number 1 , bit-shifted by an appropriate amount, to the result.

In accordance with the community guidelines on homework questions , I will not provide the full solution to your problem at this time. However, I can add code later, if required.

I am now writing a program that requires me to convert a string such as "1110011111100111" into an int string

There is no such thing as an "int string" in C. A string is a sequence of one or more char values ending with a zero char. That's the value zero ( '\0' ), not the decimal digit zero ( '0' ). Although you could form an analogous sequence of int s in an array, the terminology "int string" is not in general use.

Moreover, char is an integer data type already, so in that sense, you already have such an array of integers. You can access the digits by indexing into the string, such as with ch[2] And if you want to convert the numeric value of thereby obtained into the integer value of the digit it represents, then you can make use of the fact that C requires the decimal digits' codes to be contiguous and in ascending order, so you can perform the wanted conversion by subtracting the value of the digit 0: ch[2] - '0' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM