2015-02-09 61 views
0

我有一個十六進制值的數組,如果我想從某個特定的十六進制值中提取數值,我該怎麼做?說十六進制值是08?我想將其轉換爲int?謝謝!我的數組聲明爲: uint8_t *數組= NULL; 並填充用FREAD()如何從數組中的一個字節中取出數值C

我得到這些線以下警告assignment makes pointer from integer without a cast

int a; 
int i; 
for(i =0; i < array_size; i++){ 
    a = (int)array[i] 
} 
+0

十六進制數分配的更容易理解你說的是持有在他們的十六進制值字符串數組? – 2015-02-09 00:34:56

+0

對不起,我宣佈數組爲uinnt8_t,我甚至需要轉換或自動執行它嗎? – sudobangbang 2015-02-09 00:39:52

+0

認爲您可能將十六進制值存儲爲字符數組(字符串)而不是int值。請看看我使用'strtol'的答案,看看是否有幫助。 – 2015-02-09 01:42:21

回答

0

如果您有存儲爲string十六進制值的數組,你可以簡單地使用strtol將它們轉換爲例如:

int decimal = (int)strtol("bbccddf0", NULL, 16); 

如果您的十六進制值不爲string存儲,強制轉換爲int應該實現你在找什麼。例如:

int int_variable = (int) hex_variable; 
+0

該數組被聲明爲uint8_t *數組。這些會是字符串嗎?我用fread() – sudobangbang 2015-02-09 00:36:15

+0

不,無符號整數長度爲8位 – 2015-02-09 00:37:16

+0

那麼它會自動將它轉換爲一個int?對不起,我是新來的C. – sudobangbang 2015-02-09 00:38:08

0

在C中,十六進制值前面有0x。例如0xF等於15,0xFF等於255. 用於將十六進制變量分配給整數變量,運算符'='將執行該作業。例如a [4] = b [4],whare a是整數,b是十六進制。

我的代碼的例子在C

#include<stdio.h> 
void main(void) 
{ 
    int x = 0xF; 
    /* it also works as declaring unsigned x = 0xF */ 

    printf("x in hex = %x\n", x); 
    int a = x; /* you can declare an variable value and assign to it the hex variable */ 

    printf("a = %d\n", a); /* the output is : 15 */ 
    printf("x = %d", x); /* the output is : 15 */ 
} 
相關問題