C:将十六进制字符串解析为无符号长整数

时间:2015-04-08 09:59:54

标签: c casting long-integer

我有一个带IP地址的字符串:

  

char * input_string =" fe80000000000000022318fffeedef59&#34 ;;

我需要将其转换为unsigned long:

  

unsigned long l = 0xfe80000000000000022318fffeedef59;

我尝试了几个代码,但没有一个可行,这就是我所做的:

#include <stdio.h>
#include <stdlib.h>
void main() 
{
    char *input_string = "fe80000000000000022318fffeedef59";
    printf("input_string : %s\n\n", input_string);

    unsigned long l1 = strtol(input_string, NULL, 16);
    printf("unsigned long l1 = strtol(input_string, NULL, 16) : l1 = %lx \n\n", l1);

    unsigned long l2 = atol(input_string);
    printf("unsigned long l2 = atol(input_string) : l2 = %lx \n\n", l2);
}

输出:

  

input_string:fe80000000000000022318fffeedef59

     

unsigned long l1 = strtol(input_string,NULL,16):l1 = 7fffffffffffffff

     

unsigned long l2 = atol(input_string):l2 = 0

4 个答案:

答案 0 :(得分:3)

您的输入字符串(32字节)对应一个非常大的十六进制数字。为了保存一个32字节的字符串,你需要一个16字节(128位)的数据类型。但无符号长只有8个字节。这就是你得到这样一个输出的原因。

答案 1 :(得分:1)

作为@Weather Vane注释:“你需要一个128位数字变量来保存32字节十六进制字符串中的值”

可以使用sscanf()保存为2个64位整数。

#include <stdio.h>
#include <stdint.h>
int main(void) {
  uint64_t add[2];
  char *input_string = "fe80000000000000022318fffeedef59";
  if (2 != sscanf(input_string, "%16" SCNx64 "%16" SCNx64, &add[0], &add[1])) {
    return -1;
  }
  printf("%016" PRIx64 "%016" PRIx64 , add[0], add[1]);
  return 0;
}

答案 2 :(得分:0)

你的号码太长了。您应该检查函数strtol和atol的返回状态。

答案 3 :(得分:0)

这是我最终完成的事情。

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

void big_hex_string_to_long_array(char *);

void main( void)
{
    char *ipv6_string = "FE800000000000000202B3FFFE1E8329";

    big_hex_string_to_long_array(ipv6_string);

}

/*
  * The ipv6 address is 128 bits long, too much to process in a single block.
  * Here I split the address in four equals parts of 32 bits which can be stored
  * in an long int (4 Bytes).
  */
void big_hex_string_to_long_array(char * hex_string)
{
    // create two buffers, one for the most significants bytes and the other for the lowest significants bytes
    char buf_1[8+1];    // +1 : the space for '\0'
    char buf_2[8+1];
    char buf_3[8+1];
    char buf_4[8+1];

    // copy each half in each buffers and add ending character
    memcpy(buf_1, &(*hex_string), sizeof(char)* 8);     // copy the 8 first characters in buf_1
    buf_1[8]='\0';                      // set the last character 
    memcpy(buf_2, &(*hex_string)+8, sizeof(char)* 8);   // copy the 8 next characters in buf_2
    buf_2[8]='\0';                      //...                       
    memcpy(buf_3, &(*hex_string)+16, sizeof(char)* 8);
    buf_3[8]='\0';
    memcpy(buf_4, &(*hex_string)+24, sizeof(char)* 8);
    buf_4[8]='\0';

    printf("\nchar arrays : \nbuf1 = %s\nbuf2 = %s\nbuf3 = %s\nbuf4 = %s\n",buf_1, buf_2, buf_3, buf_4);

    // store each buffer as unsigned long
    long l1 = strtol(buf_1, NULL, 16);      // convert string to long
    long l2 = strtol(buf_2, NULL, 16);
    long l3 = strtol(buf_3, NULL, 16);
    long l4 = strtol(buf_4, NULL, 16);

    printf("\nlong int : \nl1 = %lx\nl2 = %lx\nl3 = %lx\nl4 = %lx\n",l1, l2, l3, l4);
}

<强>输出

  

char数组:

     

buf1 = FE800000

     

buf2 = 00000000

     

buf3 = 0202B3FF

     

buf4 = FE1E8329

     

long int:

     

l1 = fffffffffe800000

     

l2 = 0

     

l3 = 202b3ff

     

l4 = fffffffffe1e8329