C语言 在C中将Char数组转换为Long

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6687467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:08:48  来源:igfitidea点击:

Converting Char array to Long in C

cmemory

提问by AjayR

This question may looks silly, but please guide me I have a function to convert long data to char array

这个问题可能看起来很傻,但请指导我我有一个函数可以将长数据转换为字符数组

void ConvertLongToChar(char *pSrc, char *pDest)
{
    pDest[0] = pSrc[0];
    pDest[1] = pSrc[1];
    pDest[2] = pSrc[2];
    pDest[3] = pSrc[3];
}

And I call the above function like this

我像这样调用上面的函数

long lTemp = (long) (fRxPower * 1000);
ConvertLongToChar ((char *)&lTemp, pBuffer);

Which works fine. I need a similar function to reverse the procedure. Convert char array to long. I cannot use atol or similar functions.

哪个工作正常。我需要一个类似的函数来反转这个过程。将 char 数组转换为 long。我不能使用 atol 或类似的功能。

回答by Vinicius Kamakura

You can do:

你可以做:

union {
 unsigned char c[4];
 long l;
} conv;

conv.l = 0xABC;

and access c[0] c[1] c[2] c[3]. This is good as it wastes no memory and is very fast because there is no shifting or any assignment besides the initial one and it works both ways.

和访问c[0] c[1] c[2] c[3]。这很好,因为它不浪费内存并且速度非常快,因为除了初始值之外没有移位或任何分配,并且它可以双向工作。

回答by Kerrek SB

Leaving the burden of matching the endianness with your other function to you, here's one way:

将字节序与其他函数匹配的负担留给您,这是一种方法:

unsigned long int l = pdest[0] | (pdest[1] << 8) | (pdest[2] << 16) | (pdest[3] << 24);

Just to be safe, here's the corresponding other direction:

为了安全起见,这是相应的另一个方向:

unsigned char pdest[4];
unsigned long int l;
pdest[0] = l         & 0xFF;
pdest[1] = (l >>  8) & 0xFF;
pdest[2] = (l >> 16) & 0xFF;
pdest[3] = (l >> 24) & 0xFF;

Going from char[4]to long and back is entirely reversible; going from long to char[4]and back is reversible for values up to 2^32-1.

char[4]长到回是完全可逆的;char[4]对于高达 2^32-1 的值,从 long 到back 是可逆的。

Note that all this is only well-defined for unsigned types.

请注意,所有这些仅适用于无符号类型。

(My example is littleendian if you read pdestfrom left to right.)

(如果你从左到右阅读,我的例子是pdest。)

Addendum:I'm also assuming that CHAR_BIT == 8. In general, substitute multiples of 8 by multiples of CHAR_BITin the code.

附录:我也假设CHAR_BIT == 8. 一般来说,用CHAR_BIT代码中的倍数代替 8 的倍数。

回答by Etienne de Martel

A simple way would be to use memcpy:

一个简单的方法是使用 memcpy:

char * buffer = ...;
long l;
memcpy(&l, buff, sizeof(long));

That does not take endianness into account, however, so beware if you have to share data between multiple computers.

但是,这并没有考虑字节序,因此如果您必须在多台计算机之间共享数据,请务必小心。

回答by phoxis

If you mean to treat sizeof (long)bytes memory as a single long, then you should do the below:

如果您打算将sizeof (long)字节内存视为单个 long,那么您应该执行以下操作:

char char_arr[sizeof(long)];
long l;

memcpy (&l, char_arr, sizeof (long));

This thing can be done by pasting each bytes of the long using bit shifting ans pasting, like below.

这可以通过使用位移和粘贴来粘贴 long 的每个字节来完成,如下所示。

l = 0;
l |= (char_arr[0]);
l |= (char_arr[1] << 8);
l |= (char_arr[2] << 16);
l |= (char_arr[3] << 24);

If you mean to convert "1234\0" string into 1234L then you should

如果您想将 "1234\0" 字符串转换为 1234L 那么您应该

l = strtol (char_arr, NULL, 10); /* to interpret the base as decimal */

回答by phoxis

Does this work:

这是否有效:

#include<stdio.h>

long ConvertCharToLong(char *pSrc) {
    int i=1;
    long result = (int)pSrc[0] - '0';
    while(i<strlen(pSrc)){
         result = result * 10 + ((int)pSrc[i] - '0');
         ++i;
    }
    return result;
}


int main() {
    char* str = "34878";
    printf("The answer is %d",ConvertCharToLong(str));
    return 0;
}

回答by vtellier

This is dirty but it works:

这很脏,但它有效:

unsigned char myCharArray[8];
// Put some data in myCharArray here...
long long integer = *((long long*) myCharArray);

回答by Engineer

char charArray[8]; //ideally, zero initialise
unsigned long long int combined = *(unsigned long long int *) &charArray[0];

Be wary of strings that are null terminated, as you will end up copying any bytes beyondthe null terminator into combined; thus in the above assignment, charArrayneeds to be fully zero-initialised for a "clean" conversion.

小心空终止的字符串,因为你最终会将空终止符以外的任何字节复制到combined; 因此在上面的分配中,charArray需要完全零初始化以进行“干净”的转换。