Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C:将十六进制字符串解析为无符号长字符串_C_Casting_Long Integer - Fatal编程技术网

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

C:将十六进制字符串解析为无符号长字符串,c,casting,long-integer,C,Casting,Long Integer,我有一个带有IP地址的字符串: char*input_string=FE80000000000002218FFFEEDEF59 我需要将其转换为无符号长字符: 无符号长l=0xFE80000000000002218FFFEEDEF59 我尝试了一些代码,但都不起作用,以下是我制作的代码: #include <stdio.h> #include <stdlib.h> void main() { char *input_string = "fe800000

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

char*input_string=FE80000000000002218FFFEEDEF59

我需要将其转换为无符号长字符:

无符号长l=0xFE80000000000002218FFFEEDEF59

我尝试了一些代码,但都不起作用,以下是我制作的代码:

#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);
}
以及输出:

输入_字符串:FE80000000000002218FFFEEDEF59

无符号长l1=strtolinput_字符串,NULL,16:l1=7FFFFFFFFFFFFF

无符号长l2=atolinput\u字符串:l2=0


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

您的数字太长时间都太大了。您应该检查strtol和atol函数的返回状态。

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

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

以下是我最终完成的工作

#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);
}
输出

字符数组:

buf1=FE800000

buf2=00000000

buf3=0202B3FF

buf4=FE1E8329

长整型:

l1=FFFFFFFFE800000

l2=0

l3=202b3ff

l4=FFFFFFF E1E8329


以下是我最后所做的

#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);
}
输出

字符数组:

buf1=FE800000

buf2=00000000

buf3=0202B3FF

buf4=FE1E8329

长整型:

l1=FFFFFFFFE800000

l2=0

l3=202b3ff

l4=FFFFFFF E1E8329


注释:您需要一个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;
}

注释:您需要一个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;
}

您需要一个128位数字变量来保存32字节十六进制字符串中的值。您需要一个128位数字变量来保存32字节十六进制字符串中的值。由于您的无符号长度显然是8字节,因此可以使用其中的2个,而不是4个。此外,建议使用无符号长字符。甚至可以尝试uintmax_t.strtol return long,这样我的字符串就需要4字节长,这就是为什么我使用long数据类型来保存unsigned long,使用strtoul。要在uintmax\u t中保存,请使用strtumax。或者使用strtol需要代码吗?不,我不知道这些存在。谢谢由于无符号长显然是8字节,因此可以使用其中的2个,而不是4个。此外,建议使用无符号长字符。甚至可以尝试uintmax_t.strtol return long,这样我的字符串就需要4字节长,这就是为什么我使用long数据类型来保存unsigned long,使用strtoul。要在uintmax\u t中保存,请使用strtumax。或者使用strtol需要代码吗?不,我不知道这些存在。谢谢