1

I have a uint8_t array which should look like this .

uint8_t code[1000] = {0x66, 0xba, 0xf8, 0x03}

Now I cant hard code values in that array instead , I need to insert them one by one from char buffer[300]. The content of char buffer[300] is a space delimited string of hex values "66 ba f8 03"

The code I wrote is -

        char* token = strtok(buffer, " "); 
        // Keep printing tokens while one of the 
        // delimiters present in str[].
        int pc = 0; 
        while (token != NULL) {
            code[pc] = token; // This part is wrong. I need to cast it properly
            token = strtok(NULL, " "); 
            pc++;
        }

How can I convert a string value into a uint8_t value?

Cœur
  • 37,241
  • 25
  • 195
  • 267
sapy
  • 8,952
  • 7
  • 49
  • 60
  • I would use [`strtol`](https://linux.die.net/man/3/strtol). This comes with various error checking, and you'll have to make sure your result fits in a `uint8_t` at least too. – yano Nov 18 '19 at 17:12
  • 1
    `code[pc]` is a single `uint8_t` value, `token` is a *pointer* to an `uint8_t` value (i.e. `uint8_t *`). – Some programmer dude Nov 18 '19 at 17:12
  • Why not `char* code[1000]` instead? – tadman Nov 18 '19 at 17:15
  • If you are not against *brutal* solutions, `(uint8_t) buffer[i]` is already what you are looking for. – Roberto Caboni Nov 18 '19 at 17:19
  • @Someprogrammerdude yes that's exactly why , I couldn't come up with a solution – sapy Nov 18 '19 at 17:31
  • @Cubo78 That won't work. i.e. buffer[0] contains 6 whereas code[0] should have 0x66 . So I need to read `buffer` until I get a space, and convert all of those chars combined to uint8_t and store them to `code` – sapy Nov 18 '19 at 17:34
  • @tadman this is part of a code that will use kvm api . So we need `uint8_t code`. Can't change that – sapy Nov 18 '19 at 17:35
  • You're going to have to qualify what you're doing here, since the big picture is important. – tadman Nov 18 '19 at 17:36
  • @sapy Oh, ok. It wasn't clear from the example. Please, evise the edit I'm about to do. – Roberto Caboni Nov 18 '19 at 17:37
  • @Clifford space delimited Char values. You are right – sapy Nov 18 '19 at 17:40
  • Yes, I deleted my comment and just edited the question instead - it is clear from the use of `strtok()`, but you have to read the code to get that - and assume that the code in that respect is correct. – Clifford Nov 18 '19 at 17:49

1 Answers1

1

Example code that uses strtol to convert hex number strings.

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

int main(void)
{
    char buffer[300] = "66 ba f8 03";

    uint8_t code[1000];

    long val;
    char *endptr;

    char* token = strtok(buffer, " "); 
    // Keep printing tokens while one of the 
    // delimiters present in str[].
    int pc = 0; 
    while (token != NULL) {

        /* convert as a base16 number */
        val = strtol(token, &endptr, 16);

        /* The character following the number should be the end of the string as the input string is already tokenized. */
        if(*endptr) {
            /* error handling */
        } else if(val < 0) {
            /* error handling */
        } else if(val > UINT8_MAX) {
            /* error handling */
        } else {
            /* The checks above make sure that the value fits into uint8_t. */
            code[pc] = (uint8_t)val;
        }

        token = strtok(NULL, " "); 
        pc++;
    }

    for(int i = 0; i < pc; i++) {
        printf("code[%d] = 0x%02x\n", i, code[i]);
    }

    return 0;
}

The error handling depends on the rest of your program.

Notes:

strtok modifies the input string, so it must not be a const char[] or a string literal.

The loop doesn't contain a check to out-of-range access to code[pc].

Edit in the code above: A check for space in *endptr is unnecessary as space is used as the token delimiter, so we should never find a space in the result of strtok.

Bodo
  • 9,287
  • 1
  • 13
  • 29
  • In cases where the string has already been validated, the error checking may be unnecessary. That is not in the limited scope of the question. We are told what the string contains and given that pre-condition, the error checking is superfluous. That is not to say it is not advised, merely that it is more than was asked for. – Clifford Nov 18 '19 at 17:45
  • you my friend dont know how greatful I am for this code , which works like charm.if you are ever in chicago or new york . ping me . I definitely deserve a beer for this. – sapy Nov 18 '19 at 17:45