1

How can i convert a char array of number to byte array? Example:

char *digit="3224833640520308023"//long long array 

convert to:

uint8_t buff[256]= {0x2c, 0xc0, 0xe9, 0x1c, 0x32, 0xf1, 0x55, 0x37, 0};

(2c c0 e9 1c 32 f1 55 37)
8
  • If you put your code in a separate paragraph and indent each line by at least four spaces, it'll format much nicer and it'll be much easier for people to answer your question. Commented Jan 26, 2011 at 16:48
  • What is the relationship between the contents of the char array and the contents of your uint8_t array? Commented Jan 26, 2011 at 16:52
  • I take it you want to accept a string representing a number, convert to long long, and then have it as a uint8_t array with the binary representation? Or do you want to automatically convert it to a C statement like the one you've listed? Do you mind indulging in what is technically undefined behavior? Commented Jan 26, 2011 at 16:53
  • Is your "digit" a group of individual ASCII characters? A giant decimal string? A group of hex digits? Please clarify... Commented Jan 26, 2011 at 16:55
  • Big number (3224833640520308023) convert to hex (2cc0e91c32f15537) Commented Jan 26, 2011 at 16:57

2 Answers 2

1

I printed in reverse order in the end. You may want to endian swap if you need the array in that endian order.

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

char *digit="3224833640520308023";

int  main() {
  int i;
  unsigned char byteArray[16];
  unsigned long long x = strtoull(digit,0,10);

  printf("%llx\n",x);
  printf("%llu\n",x);
  for (i=0;i<8;i++) {
    byteArray[i] = (x>>(i*8)) & 0xFF;
  }

  printf ("Array is:\n");
  for (i=7;i>=0;i--) {
    printf("%2.2x ",byteArray[i]);
  }
  return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Instead of explicitly creating each value, you can cast a reference to an array. reinterpret_cast<uint8_t *>(&x)[i] (assuming endianness) should do the trick
my device print to display: 2cc0e91c 750840092 Array is: 00 00 00 00 2c c0 e9 1c
Well, yes - this was C (not C++) so the "reinterpret_cast" won't work - but I could have "(unsigned char *) (&x)"
0

strtoull converts the string to a 64 bits internal representation.

htobe64 will switch the endianness to big endian (the one you used in your example) if needed on your platform.

You can then copy 8 bytes from this big endian 64bit variable to your byte array.

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

char *digit="3224833640520308023";

main ()
{
    uint64_t ull;
    uint64_t beull;
    uint8_t buff[8];
    int i;

    ull=strtoull(digit,0,10);
    beull=htobe64(ull);
    memcpy(buff,&beull,8);
    for(i=0;i<8;i++)
    {
        printf("%02x\n",buff[i]);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.