0

How do I easily convert a string of characters in quotes to an array of ascii2 values?

For example:

char ST1[5] = "12345";

to

char DST1[5] = { 0x31, 0x32, 0x33, 0x34, 0x35 }; //string of bytes
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Jerbucc
  • 9
  • 2

3 Answers3

0

Practically nothing.

First, remember that string literals such as "12345" have an extra "null" byte at the end. So instead of treating this array initialization of "12345" to an array of 5 elements, it's really an array of 6 elements, so this initialization statement is more appropriate:

char ST1[6] = "12345";

Or just simply:

char ST1[] = "12345"; // where the size of "6" is auto inferred.

Which is equivalent to:

char DST1[] = {0x31,0x32,0x33,0x34,0x35,0x00};

And once you have an array of 6 elements, you can pass it around as if it had only 5:

So if you have some code that expects to operate on an array of 5 chars:

 void foo(char DST1[])
 {
     for(int i = 0; i < 5; i++)
     { 
         process(DST1[i]);
     }
 }

You can invoke it as follows:

char ST1[]="12345";
foo(ST1);
selbie
  • 100,020
  • 15
  • 103
  • 173
  • Thanks everyone, I meant ASCII didn't need the extra 2... I need to send the actual bytes to a UART so I suppose I could use sprintf(DST1[i]"%d\n",(int)ST1[i]); in the loop. – Jerbucc Mar 10 '19 at 08:49
  • `memcpy(DST, ST1, count*sizeof(*DST));` – selbie Mar 10 '19 at 10:27
  • The `sprintf` loop you propose will literally copy `"4950515253"` into your buffer. – selbie Mar 10 '19 at 10:30
  • But why copy ST1 into DST1 when you can just pass ST1 into the UART API. – selbie Mar 10 '19 at 10:36
  • Actually as Selbie said, all I needed to do was for(i=5;i>0;i--)DST1[i]=ST1[i]; and that worked fine. – Jerbucc Mar 10 '19 at 16:57
  • Just saw your post- The UART subroutine outputs a buffer called Data_Out[], so i need to put the string of characters in that buffer. So actually what I did was.. for(i=5;i>0;i--)Data_Out[i]=ST1[i]; and that worked, Are you saying there is an easier way to put the string into the buffer? – Jerbucc Mar 10 '19 at 17:05
  • Oh I see. Then the `memcpy` statement in my comment above will suffice. – selbie Mar 10 '19 at 17:43
0

One for all, one answer is casting. Easy way to do it:

int main () {
    int i;
    char ST1[5]="12345";
    for (i=0;i<5;i++)
        printf("%d\n",(int)ST1[i]);
    return 0;
}

Just like I printed, you can store it, calculate using it or anything possible. And as I see, you want those number in hexadecimals. For that, just change the printf's placeholder with help from here - printf() formatting for hex

0

The only difference between the definitions of ST1 and DST1 is the silent assumption that the target character set is ASCII, at least for the digits.

Here is another alternative:

char CST1[5] = { '1', '2', '3', '4', '5' };

Note however that none of these char arrays are proper C strings because they all lack a null terminator. If you with to define an array that is a C string, you should use this syntax:

char SST1[] = "12345";

Notice the missing length between the []: the compiler will determine the length of the array from the initializer and add a null terminator, hence a length a 6.

C strings can be copies with strcpy, char arrays that do not have a null terminator should be copied with memcpy or memmove.

chqrlie
  • 131,814
  • 10
  • 121
  • 189