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

  • Does this answer your question? [How to convert a string to integer in C?](https://stackoverflow.com/questions/7021725/how-to-convert-a-string-to-integer-in-c) – joshmeranda Oct 02 '21 at 02: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 – Heavenguard.01 Oct 02 '21 at 03:01
  • Do you want to convert by yourself or third party lib? – Hồng Phúc Oct 02 '21 at 03: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 – joshmeranda Oct 02 '21 at 03: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. – Andreas Wenzel Oct 02 '21 at 03:06
  • @HồngPhúc It doesn't matter. I'd like to choose the easiest way – Heavenguard.01 Oct 02 '21 at 03:08
  • @AndreasWenzel Sorry for the ambiguity, I've edited my post and added an example. I hope that would help. – Heavenguard.01 Oct 02 '21 at 03:17

2 Answers2

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.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
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'.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157