Reputation: 857
Is there any short code convert a string of unsigned long long to uint32_t[]?
eg, 11767989860 => uint32_t[] {0xaaaa, 0xbbbb}?
Upvotes: -2
Views: 513
Reputation: 4044
You have 11767989860 but in string form, and you want it to break into integer array {0x2,0xBD6D4664}
.
You can first convert string to long long, then copy 4 bytes from that long long variable into your integer array.
Below is the sample program
#include<stdio.h>
#include<stdint.h>
int main()
{
unsigned long long ll = 0;
uint32_t arr[2];
char str[]="11767989860";
char *tmpPtr = NULL;
tmpPtr = ≪
sscanf(str,"%llu",&ll);
printf("ll=%llu",ll);
/*Big endian*/
memcpy(arr,&ll,sizeof(ll));
printf("\n%u %u\n",arr[0],arr[1]);
/*Little endian*/
memcpy(&arr,&tmpPtr[4],sizeof(ll)/2);
memcpy(&arr[1],&tmpPtr[0],sizeof(ll)/2);
printf("\n%u %u\n",arr[0],arr[1]);
return 0;
}
Upvotes: 0
Reputation: 735
Ignoring overflow, this gives you an unsigned long long (which I believe is 64-bit, not 32):
unsigned long long r = 0;
while (*pstr >= '0' && *pstr <= '9') r = r * 10 + (*pstr++ - '0');
Upvotes: 0