简体   繁体   English

在 C 中将 Char 数组转换为 Long

[英]Converting Char array to Long in C

This question may looks silly, but please guide me I have a function to convert long data to char array这个问题可能看起来很傻,但请指导我我有一个 function 将长数据转换为 char 数组

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我这样称呼上面的 function

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

Which works fine.哪个工作正常。 I need a similar function to reverse the procedure.我需要一个类似的 function 来扭转这个过程。 Convert char array to long.将 char 数组转换为 long。 I cannot use atol or similar functions.我不能使用 atol 或类似的功能。

You can do:你可以做:

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

conv.l = 0xABC;

and access c[0] c[1] c[2] c[3] .并访问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.这很好,因为它不会浪费 memory 并且速度非常快,因为除了初始值之外没有移位或任何分配,并且它可以双向工作。

Leaving the burden of matching the endianness with your other function to you, here's one way:将字节顺序与您的其他 function 匹配的负担留给您,这是一种方法:

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;char[4]到 long 和 back 是完全可逆的; going from long to char[4] and back is reversible for values up to 2^32-1.从 long 到char[4]并返回对于高达 2^32-1 的值是可逆的。

Note that all this is only well-defined for unsigned types.请注意,所有这些仅对无符号类型进行了明确定义。

(My example is little endian if you read pdest from left to right.) (如果您从左到右阅读pdest ,我的示例是端序。)

Addendum: I'm also assuming that CHAR_BIT == 8 .附录:我还假设CHAR_BIT == 8 In general, substitute multiples of 8 by multiples of CHAR_BIT in the code.通常,在代码中将 8 的倍数替换为CHAR_BIT的倍数。

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.但是,这并没有考虑字节序,因此如果您必须在多台计算机之间共享数据,请小心。

If you mean to treat sizeof (long) bytes memory as a single long, then you should do the below:如果您打算将sizeof (long) bytes memory 视为单个 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 */

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;
}

This is dirty but it works:这很脏,但它有效:

unsigned char myCharArray[8];
// Put some data in myCharArray here...
long long integer = *((long long*) myCharArray);
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 beyond the null terminator into combined ;警惕 null 终止的字符串,因为您最终会将 null 终止符之外的任何字节复制到combined中; thus in the above assignment, charArray needs to be fully zero-initialised for a "clean" conversion.因此在上述分配中, charArray需要完全零初始化以进行“干净”转换。

Just found this having tried more than one of the above to no avail:=(:刚刚发现这已经尝试了上述一种以上无济于事:=(:

    char * vIn = "0";
    long vOut = strtol(vIn,NULL,10);

Worked perfectly for me.非常适合我。 To give credit where it is due, this is where I found it: https://www.convertdatatypes.com/Convert-char-Array-to-long-in-C.html为了给予应得的荣誉,这是我发现它的地方: https://www.convertdatatypes.com/Convert-char-Array-to-long-in-C.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM