簡體   English   中英

從c語言中的char數組中獲取int值

[英]get int value from array of char in c language

我有一系列字符,如:

char bytes[8]={2,0,1,3,0,8,1,9}

我想從下面的數組中獲取前四個字符,並將它們放入一個新的整數變量中。 我怎樣才能做到這一點? 我試圖改變它們,但這種邏輯不起作用。 任何想法? 謝謝。

示例:從此數組中獲取:年月日

char bytes[8]={2,0,1,3,0,8,1,9}

int year = 2013 ......  month = 8 ............  day = 19

而不是左移與<<運算符(或多或少等於乘以2^N ),你應該乘以10^N 您可以這樣做:

int year = bytes[0] * 1000 +
           bytes[1] * 100 +
           bytes[2] * 10 +
           bytes[3];

int month = bytes[4] * 10 +
            bytes[5];

int day = bytes[6] * 10 +
          bytes[7];

當然,您可以使用循環來使代碼更具可讀性(如有必要)。

enum {
   NB_DIGITS_YEAR = 4,
   NB_DIGITS_MONTH = 2,
   NB_DIGITS_DAY = 2,
   DATE_SIZE = NB_DIGITS_YEAR + NB_DIGITS_MONTH + NB_DIGITS_DAY
};

struct Date {
   int year, month, day;
};

int getDateElement(char *bytes, int offset, int size) {
   int power = 1;
   int element = 0;
   int i;

   for (i = size - 1; i >= 0; i--) {
      element += bytes[i + offset] * power;
      power *= 10;
   }

   return element;
}

struct Date getDate(char *bytes) {
   struct Date result;
   result.year = getDateElement(bytes, 0, NB_DIGITS_YEAR);
   result.month = getDateElement(bytes, NB_DIGITS_YEAR, NB_DIGITS_MONTH);
   result.day = getDateElement(bytes, NB_DIGITS_YEAR + NB_DIGITS_MONTH, NB_DIGITS_DAY);
   return result;
}

使用這最后一個代碼,更容易更改以bytes存儲的日期格式。

例:

int main(void) {
   char bytes[DATE_SIZE] = {2, 0, 1, 3, 0, 8, 1, 9};
   struct Date result = getDate(bytes);
   printf("%02d/%02d/%04d\n", result.day, result.month, result.year);
   return 0;
}

輸出:

19/08/2013

你想要這個嗎?

int year  = byte[0] * 1000 + byte[1] * 100 + byte[2] * 10 + byte[3];
int mounth = byte[4] * 10 + byte[5];
int day =  byte[6] * 10 + byte[7];

注意 :這是有效的,因為整數是實際的數字值,而不是數字的字符代碼,例如index = 0的byte[]值是2而不是'2'

所以假設你有一些char值,如:

char bytes[8]={'2', '0', '1', '3', '0', '8', '1', '9'};

然后將此代碼更改為:

#define digit(d)  ((d) - ('0'))

int year  =  digit(byte[0]) * 1000 + 
             digit(byte[1]) * 100 + 
             digit(byte[2]) * 10 + 
             digit(byte[3]);
int mounth = digit(byte[4]) * 10 + digit(byte[5]);
int day =  digit(byte[6]) * 10 + digit(byte[7]);

從零開始。 添加第一個數字。 乘以十。 添加第二個數字。 乘以十。 添加第三個數字。 乘以十。 添加第四個數字。 現在你有一個四位整數。

我不熟悉特定的c語法,但我可以傳達這個想法。 將字符連接在一起形成一個字符串,然后使用parseInt方法(假設c有一個)從中獲取一個整數。 或者,您可以通過乘以10的冪來移動字符。例如,給定{2,0,1,3} :( (2 * 10^3) + (0 * 10^2) + (1 * 10^1) + (3 * 10^0)

這樣的東西會起作用:

 int month;
 int year;
 int year = ((int)bytes[0]*1000);
 year += (int)bytes[1]*100);
 year += ((int)bytes[2] *10);
 year += ((int)bytes[3]);

 if(sizeof(bytes) == 9){ 
  //month is >10
   month = ((int)bytes[4] *10);
   month += ((int)bytes[5]);
}

..等等

這是另一種方法,主要使用標准庫中的字符串函數。

我沒有測試過這段代碼,但它應該給你一個想法。

    #include <string.h>
    int main (void) {
      char bytes[8]={2,0,1,3,0,8,1,9};
      char tempstring[8], * end;
      unsigned int i, year, month, day;
      char zero = "0"; //we need to know the difference between 0 and "0"
      for (i = 0; i < 8; i++) {
        bytes[i] += (int)zero;
      }
  //now the numbers are the equivalent characters, but we need to split them out
  //and convert them 
  strncopy(tempstring, bytes, 4);
  year = strtoul(tempstring, &end, 10);
  strncopy(tempstring , bytes + 4, 2);
  month = strtoul(tempstring, &end, 10);
  strncopy(tempstring , bytes + 6, 2);
  day = strtoul(tempstring, &end, 10);

  return 0;
  }

暫無
暫無

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

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