Reputation: 11
I have a string like this "10100010" that's equal to 162 decimal or 0xA2 hex. I need to fin a way to calculate one of those values in C. Or just a way to obtain this in C : 0b(string) - 0b10100010
Ive tried to convert each bit (char) to int by doing this : String[0] - '0' , and use a loop to mannualy calculate the value of the binary number, something like this, but it didn't work..
for (i=0,n=n_c; i<n_c; i++,n--)
{
decimal_value=decimal_value+ pow( (val[i]-'0') , (n-1) );
printf("%d ", pow( (int) ((val[i]-'0')) , (n-1)));
}
in this particular case n_c = 6 (6 bits). If the string is bigger than 8 bits, I have another problem, but let's focus on this "simple" case for now.
could you help me?
Upvotes: 0
Views: 1627
Reputation: 153348
Best to use strtol()
, yet if you want to roll your own code:
An easy way to convert a string of base N characters into an integer to walk though each digit and add it to the sum which has been multiplied by the base.
OP will still needed to add some code and determine how to handle bad input. Something to get you started.
// convert '0' --> 0, 'A' --> 10, 'z' --> 35, etc.
// return -1 on failure
int covert_char_to_digit(char ch) {
TBD code
}
int string_to_int(const char *s, int base) {
if (test_if_valid_base(base)) {
fprintf(stderr, "Invalid base %d\n", base);
return 0;
}
int sum = 0;
while (*s) {
int ch = covert_char_to_digit(*s);
if (ch < 0 || ch >= base) {
fprintf(stderr, "Invalid digit\n");
return 0;
}
// Does OP want to detect pending overflow
// Here is a good place for such code.
sum = sum*base + ch;
s++;
}
return sum;
}
Upvotes: 0
Reputation: 21
The reason why your method does not work is 1. You need make use of (i)*2^(n-1)
2. You make use of wrong way to print the result.The pow function's definition is double pow(double x, double y);
Pls try this:
#include <stdio.h>
#include <math.h>
int main()
{
int i = 0;
int n_c = 6;
char val[10]={'0','1','0','1','0','1','0',};
double decimal_value = 0;
int n;
for (i=0,n=n_c; i<n_c; i++,n--)
{
decimal_value=decimal_value+ pow( (val[i]-'0')*2 ,(n-1) );
printf("%lf \n", pow( (((val[i]-'0'))*2) , (n-1)));
}
printf("%lf \n", decimal_value);
}
Upvotes: 0
Reputation: 514
there is a example of bin to dec:
#include <stdlib.h>
#include <stdio.h>
int main(){
char a[] = "100";
char b[] = "100";
char c[] = "0x11";
int x, y, z;
x = strtol( a, NULL, 10 );
y = strtol( b, NULL, 2 );
z = strtol( c, NULL, 16 );
printf( "x = %d\n", x );
printf( "y = %d\n", y );
printf( "z = %d\n", z );
}
output:x = 100 ;y = 4 ;z = 17
Upvotes: 2
Reputation: 19221
I think using bit operations might get you a better result.
The following code isn't the most optimized nor creative, but demonstrates a direct approach to the problem using bitwise operators (<<
and |
).
#include <stdio.h>
/*
Returns the binary number found at the beginning of the string (if any).
No error checks are performed
*/
unsigned long parse_binary_string(char* str) {
unsigned long value = 0;
while (1) {
if (*str == '0')
value = value << 1;
else if (*str == '1')
value = (value << 1) | 1;
else
break;
str++;
}
return value;
}
int main(int argc, char const* argv[]) {
char string[] = "01101010";
printf("The result for %s is %lu\n", string, parse_binary_string(string));
return 0;
}
Upvotes: 0