-1

I have an array char input[11] = {'0','2','7', '-','1','1','2', ,'0','0','9','5'};

How do I convert input[0,1,2] to int one = 27, input[3,4,5,6] to int two = -112 and input[7,8,9,10] to int three = 95?

thx, JNK

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
JNK
  • 1,753
  • 8
  • 25
  • 37
  • 1
    How can you have a `-` in a `byte[]`? And what about the blank entry? And why does your 27 only use 3 entries? – moinudin Dec 21 '10 at 11:51
  • i'm not quite sure if its a byte or char array...:) thee array "includes" the following data heading(0->360 --> 3 digits), pitch(-180->180 --> 4 digits) and roll(-180->180 --> 4 digits) – JNK Dec 21 '10 at 11:53
  • 1
    Looks like it should be a char array and contain characters ('0','2','7', etc.) – Jonathan Wood Dec 21 '10 at 11:54
  • 1
    it would be easier for you if all your entries would either share a common break signal (e.g a blank) or are the same size (e.g. four chars long). – eckes Dec 21 '10 at 11:57
  • 1
    @eckes Sometimes you're given data in a format you have no control over. :) – moinudin Dec 21 '10 at 12:00

2 Answers2

5

You can use a combination of strncpy() to extract the character range and atoi() to convert it to an integer (or read this question for more ways to convert a string to an int).

int extract(char *input, int from, int length) {
  char temp[length+1] = { 0 };
  strncpy(temp, input+from, length);
  return atoi(temp);
}

int main() {
  char input[11] = {'0','2','7','-','1','1','2','0','0','9','5'};
  cout << "Heading: " << extract(input, 0, 3) << endl;
  cout << "Pitch:   " << extract(input, 3, 4) << endl;
  cout << "Roll:    " << extract(input, 7, 4) << endl;
}

Outputs

Heading: 27
Pitch:   -112
Roll:    95

http://ideone.com/SUutl

Community
  • 1
  • 1
moinudin
  • 134,091
  • 45
  • 190
  • 216
  • `strncpy()` does not null-terminate the destination if it copied `length` characters, so you need to pre-fill the array with `0` (or set `temp[length] = 0` after the strncpy). – caf Dec 21 '10 at 23:06
  • @caf Are you sure? If so, then why does the above code work? See the ideone link. – moinudin Dec 21 '10 at 23:09
  • It's working by accident - see the ideone link I pasted as a comment to the other question. `strncpy()` is designed for working with fixed-length string fields that are only null-terminated if they're short, rather than regular C strings (such fields were used in original UNIX directory entries). – caf Dec 21 '10 at 23:14
  • Man page for `strncpy(char *dest, const char *src, size_t n);` states that "The `strncpy()` function is similar [to `strcpy`], except that at most `n` bytes of `src` are copied. **Warning: If there is no null byte among the first `n` bytes of `src`, the string placed in `dest` will not be null-terminated.**" – Stefan van den Akker Jan 23 '15 at 10:38
0

As I understand your comment, you know that the first entry is 3 digits wide, the second and third are 4 digits wide:

// not beautiful but should work:
char buffer[5];
int  one   = 0;
int  two   = 0;
int  three = 0;
// read ONE
memcpy(buffer, input, 3); 
buffer[3] = '\0';
one = atoi(buffer);
// read TWO
input += 3;
memcpy(buffer, input, 4);
buffer[4] = '\0';
two = atoi(buffer);
// read THREE
input += 4;
memcpy(buffer, input, 4);
buffer[4] = '\0';
three = atoi(buffer);
caf
  • 233,326
  • 40
  • 323
  • 462
eckes
  • 64,417
  • 29
  • 168
  • 201
  • Why use `memcpy()` when `strncpy()` was designed for strings and handles the null-terminating character for you? – moinudin Dec 21 '10 at 12:14
  • ack. didn't have in mind that `strncpy()` adds the trailing \0. Fixing the answer. Thanks. – eckes Dec 21 '10 at 12:23
  • Just be aware that strncpy() does not always produce a string. If the source hast the same length as the len param you pass in, no 0 terminator is added – nos Dec 21 '10 at 16:03
  • @nos Yup, but in this case the string is of known length. – moinudin Dec 21 '10 at 17:05
  • 1
    @marcog yes, and strncpy(buffer, input, 3); will leave buffer *not* 0 terminated given the array the op has in his question. Which means calling atoi on buffer is quite dangerous – nos Dec 21 '10 at 17:16
  • @nos Which array are you referring to? The code works just fine: http://ideone.com/dV6wU – moinudin Dec 21 '10 at 17:25
  • @marcog: Your code is reading uninitialised data from `buffer[3]` and `buffer[4]` (it's only working because that uninitialised data is happening to work fine) - you can see the error if you initialise `buffer` with: `char buffer[5] = { '9', '9', '9', '9', '9' };` [(see on ideone)](http://ideone.com/9vrSp). – caf Dec 21 '10 at 23:11