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
CodePudding user response:
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.
CodePudding user response:
Please use this for your situation:
#include <stdio.h>
#include <stdlib.h>
int charToInt(char ch) {
return ch - 48;
}
int power(int x, int y) {
if (y == 0) return 1;
int result = 1;
for(int i =0; i<y;i ) {
result*=x;
}
return result;
}
int stringToInt(char* numberInString, int startIndex, int endIndex) {
int intNumber = 0;
// get length
int len = 0;
while (numberInString[len] != '\0') {
len ;
}
// convert to int but in binary term
int *binary = (int*) malloc(len * sizeof(int));
for(int i =len-1;i > -1; i--) {
binary[i] = charToInt(numberInString[i]);
// printf("%i", binary[i]);
}
// convert binary to dec
for(int i = 0; endIndex-i-1 >= startIndex; i ) {
int value = binary[endIndex-i-1]*power(2,i);
intNumber = value;
}
return intNumber;
}
int main()
{
char* ch = "1110011111100111";
printf("%i", stringToInt(ch,0,3));
return 0;
}
CodePudding user response:
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'
.