0

i wanted to split this hexstring, convert em, and store em in an array. but it seems there is something off from my work, and i don't know what.

I intend to split this string

27CA6B

to

 27
 CA
 6B

but the output is always only the first string. like

27
51819
0

please somebody help, here is my code

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

int main(void)
 {
char bc[] = "27CA6B";
char *theEnd;

long result;
long resulta;
long resultb;
long resultc;

result = strtol (bc, &theEnd, 0);
resulta = strtol (theEnd, &theEnd, 16 );
resultb = strtol (theEnd, NULL, 0);

//int i = 0;
//printf("%c%c%c%c%c%c\n", bc[0], bc[1], bc[2], bc[3], bc[4], bc[5]);

printf("%ld\n", result, &bc[0]);
printf("%ld\n", resulta, &bc[1]);
printf("%ld\n", resultb, &bc[2]);


return 0;
 }
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
user6318361
  • 51
  • 1
  • 7
  • Note: `CA6B` is valid as whole in hex. – Sourav Ghosh Nov 09 '16 at 09:17
  • yes, its a the whole CA6B in decimal, but i wanted it to be separated away like my intention – user6318361 Nov 09 '16 at 09:26
  • What exactly is the requirement? Your first paragraph makes it seem you want to split a string of 6 characters into 3 strings with two characters each, but your code looks like you have to convert a decimal number, followed by a hex number, followed by another decimal one. Are the lengths fixed? Are there no separators? – Karsten Koop Nov 09 '16 at 09:28
  • Please indent your code. – Jabberwocky Nov 09 '16 at 09:30
  • the general purpose is i want to split a string with 6 characters into 3 string with 2 char each one, then i want to convert them to int using strtol and store em in array. no, no separator, nothing at all. if my code looks confusing, dont mind,, im just blankly try everything to do my goal because im new in c, thats why my code looks strange – user6318361 Nov 09 '16 at 09:32
  • @MichaelWalz thats my whole code – user6318361 Nov 09 '16 at 09:33
  • Read [this](https://en.wikipedia.org/wiki/Indent_style). – Jabberwocky Nov 09 '16 at 09:37

2 Answers2

1

The "issue" that you see in because of this line

 resulta = strtol (theEnd, &theEnd, 16 );

there, theEnd points to CA6B which, as per the base 16, is a valid input as whole, so the whole string is consumed and converted. The decimal representation value is 51819, which you see as output.

The best way to achieve this would be (based on your approach) to take a pointer to the starting of the array, and "clip" it on alternate indexes.

That said, all your printf() statements are logically wrong, as you have only one format specifier but you supply two arguments, so as the last one will be ignored silently.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
    char bc[] = "27CA6B";
    unsigned char result[(sizeof(bc)-1)/2] = {0};
    int i = 0;

    for(char *p = bc; *p; p += 2){
        char part[3] = {0};
        memcpy(part, p, 2);//Extract 2 characters
        result[i++] = strtoul(part, NULL, 16);
    }

    for(i = 0; i < sizeof(result); ++i){
        printf("%3u %c%c\n", result[i], bc[i*2], bc[i*2+1]);
    }

    return 0;
}

expand loop

unsigned char result1,result2,result3;
int i = 0;
char part[3] = {0};

memcpy(part, bc + i, 2); i += 2;
result1 = strtoul(part, NULL, 16);
memcpy(part, bc + i, 2); i += 2;
result2 = strtoul(part, NULL, 16);
memcpy(part, bc + i, 2); i += 2;
result3 = strtoul(part, NULL, 16);

i = 0;
printf("%3u %c%c\n", result1, bc[i], bc[i+1]); i += 2;
printf("%3u %c%c\n", result2, bc[i], bc[i+1]); i += 2;
printf("%3u %c%c\n", result3, bc[i], bc[i+1]); i += 2;
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70