0

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

7
  • Does this answer your question? How to convert a string to integer in C? Commented Oct 2, 2021 at 2:59
  • Not really, my intention was to convert it to a string so that I can get access to each index, rather than make it to an integer number Commented Oct 2, 2021 at 3:01
  • Do you want to convert by yourself or third party lib? Commented Oct 2, 2021 at 3:02
  • @Heavenguard.01 Oh ok, it already is a string. In C there is not "string" type, just a pointer to an array of characters Commented Oct 2, 2021 at 3:03
  • Your question is unclear. Is your question maybe the following? "How do I convert a string representing a number in binary format into a string representing a number in decimal format?" Please give an example of what the input should be and what the output should be. Commented Oct 2, 2021 at 3:06

2 Answers 2

2

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 (i.e. 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 (i.e. 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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 ints 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'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.