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
我有一个像这样的字符串"10100010"它等于162个小数或0xA2十六进制。我需要找到一种方法来计算C中的其中一个值,或者用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..
我试着将每个比特(char)转换为int,方法是:字符串[0]- '0',然后使用一个循环来计算二进制数的值,类似这样的东西,但它不起作用。
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.
在这种情况下,n_c = 6(6位)。如果字符串大于8位,我还有另一个问题,但是现在让我们关注这个“简单”的例子。
could you help me?
你能帮我吗?
4 个解决方案
#1
2
there is a example of bin to dec:
有一个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
输出:x = 100,y = 4,z = 17
#2
0
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 |
).
下面的代码不是最优化的,也不是最有创意的,但是演示了使用位运算符(< <和|)直接解决问题的方法。< p>
#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;
}
#3
0
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.你的方法不能工作的原因是1。你需要利用(i)* 2 ^(n - 1)2。你用错误的方式打印结果。
The pow function's definition is double pow(double x, double y);
pow函数的定义为双pow(双x,双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);
}
#4
0
Best to use strtol()
, yet if you want to roll your own code:
最好使用strtol(),但是如果您希望滚动自己的代码:
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.
一种简单的方法,将一串以N为基数的字符转换成一个整数,遍历每个数字并将其添加到已乘以基数的和中。
OP will still needed to add some code and determine how to handle bad input. Something to get you started.
OP仍然需要添加一些代码并确定如何处理糟糕的输入。让你开始的东西。
// 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;
}
#1
2
there is a example of bin to dec:
有一个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
输出:x = 100,y = 4,z = 17
#2
0
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 |
).
下面的代码不是最优化的,也不是最有创意的,但是演示了使用位运算符(< <和|)直接解决问题的方法。< p>
#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;
}
#3
0
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.你的方法不能工作的原因是1。你需要利用(i)* 2 ^(n - 1)2。你用错误的方式打印结果。
The pow function's definition is double pow(double x, double y);
pow函数的定义为双pow(双x,双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);
}
#4
0
Best to use strtol()
, yet if you want to roll your own code:
最好使用strtol(),但是如果您希望滚动自己的代码:
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.
一种简单的方法,将一串以N为基数的字符转换成一个整数,遍历每个数字并将其添加到已乘以基数的和中。
OP will still needed to add some code and determine how to handle bad input. Something to get you started.
OP仍然需要添加一些代码并确定如何处理糟糕的输入。让你开始的东西。
// 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;
}