簡體   English   中英

如何將二進制數轉換為有符號十進制數

[英]How do I convert binary number to signed decimal

假設我有這個二進制數“1011”,我想把它轉換成有符號十進制,所以它是“-3”而不是“11”。 我該怎么做我 C.

十進制、二進制、十六進制、八進制……只是字符串表示的問題; 無論我們使用什么基數,整數值(例如存儲在int變量中)都保持不變。

例如

int a=33;   // understood as decimal representation by the compiler
int b=0x21; // understood as hexadecimal representation by the compiler
int c=041;  // understood as octal representation by the compiler
int d='!';  // understood as ASCII representation by the compiler
if((a==b)&&(a==c)&&(a==d))
{
  printf("this condition is always true!\n");
}

然后,我們可以從問題中假設"1011"是以 2 為基數表示的 4 位有符號整數的字符串表示,並且我們想要生成另一個包含以 10 為基數表示的相同值的表示的字符串。

假設我們依賴於廣泛使用的二進制補碼表示,這里是一個循序漸進的例子(可能存在一些更有效的方法來做同樣的事情)。

#include <stdio.h>
#include <string.h>
#include <assert.h>

int
read_bin_value(const char *bin_str)
{
  unsigned int result=0;
  const int bin_len=8*(int)sizeof(result);
  const int str_len=(int)strlen(bin_str);
  assert(str_len&&(str_len<=bin_len));
  // extract bits from bin_str
  for(int i=0; i<str_len; ++i)
  {
    const unsigned int bit=(bin_str[i]!='0'); // assume '1' if not '0'
    result|=(bit<<(str_len-1-i)); // place bit
  }
  // extend sign bit
  const unsigned int sign_bit=(bin_str[0]!='0');
  for(int i=str_len; i<bin_len; ++i)
  {
    result|=(sign_bit<<i);
  }
  // interpret as signed value
  return (int)result;
}

int
main(void)
{
  const char *bin_str[]={"1011", "0011", "0", "1", "01", NULL};
  for(int i=0; bin_str[i]; ++i)
  {
    char dec_str[20];
    snprintf(dec_str, sizeof(dec_str), "%d", read_bin_value(bin_str[i]));
    printf("%s --> %s\n", bin_str[i], dec_str);
  }
  return 0;
}

請注意,根據二進制補碼,-3(基數為 10)的二進制表示不是"1011" (我猜您認為有符號整數只是帶有符號位的無符號整數,但事實並非如此)。

您必須確定要使用多少位表示並將輸入作為字符串。

#include<stdio.h>
#include<string.h>
char numberAsAString[65];
int wordSize;
int binToSignedInt(char* numberAsAString,int wordSize)
{
    int value=0,multi=1,i,upto=1; //"upto" means we will stop calculating the value before 1st element...as the 0th element is containing sign bit and it is not a part of the value
    if(wordSize>strlen(numberAsAString))upto=0; //But if the word size is larger than the signed bit is assumed to be zero and we are taking the whole input as the value
    for(i=strlen(numberAsAString)-1;i>=upto;i--)
    {
        if(numberAsAString[i]=='1') //simple bin to dec conversion
        {
            value=value+multi;
        }
        multi=multi*2;
    }
    if(upto==0)return value; //If allowed number of bits representation was larger then the number was positive...as 0 is assumed at the beginning of the representation
    //otherwise
    if(numberAsAString[0]=='1')return -value; //If sign bit is 1 the value is negative
    return value; //The value is positive if the signed bit is not one (or is 0)
}
int main()
{
    printf("Enter number: "); //Enter the number as a sting
    scanf("%s",numberAsAString);
    printf("Enter word size: "); //Enter how many bits representation you want to use
    scanf("%d",&wordSize);
    if(wordSize<strlen(numberAsAString)) //If the number is larger than the allowed bits representation than considering the input as an invalid input
    {
        printf("Invalid Input\n");
        return 0;
    }
    printf("Value in signed integer form: %d\n",binToSignedInt(numberAsAString,wordSize)); //Signed integer output
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM