1

I am writing a little program that talks to the serial port. I got the program working fine with one of these lines;

unsigned char send_bytes[] = { 0x0B, 0x11, 0x00, 0x02, 0x00, 0x69, 0x85, 0xA6, 0x0e, 0x01, 0x02, 0x3, 0xf };

However the string to send is variable and so I want make something like this;

char *blahstring;
blahstring = "0x0B, 0x11, 0x00, 0x02, 0x00, 0x69, 0x85, 0xA6, 0x0e, 0x01, 0x02, 0x3, 0xf"
unsigned char send_bytes[] = { blahstring };

It doesn't give me an error but it also doesnt work.. any ideas?

3
  • 5
    what? 0x0B is not the same as "0x0B". what are you trying to do exactly? what function are you using to send? Commented Feb 1, 2013 at 5:24
  • You'll need to write a function to parse that string. Split it at each comma and convert from hex. You'll need to count the number of bytes as well. And use std::string for both the input and the output because it will bundle the length in for you. (Or follow Aniket's answer.) Commented Feb 1, 2013 at 5:26
  • boost.org/doc/libs/1_52_0/doc/html/boost_lexical_cast.html may help. You can split your string, do a lexical_cast<int> then cast the int to an unsigned char Commented Feb 1, 2013 at 5:28

2 Answers 2

7

a byte-string is something like this:

char *blahString = "\x0B\x11\x00\x02\x00\x69\x85\xA6\x0E\x01\x02\x03\x0f"

Also, remember that this is not a regular string. It will be wise if you explicitly state it as an array of characters, with a specific size:

Like so:

unsigned char blahString[13] = {"\x0B\x11\x00\x02\x00\x69\x85\xA6\x0E\x01\x02\x03\x0f"};
unsigned char sendBytes[13];
memcpy(sendBytes, blahString, 13); // and you've successfully copied 13 bytes from blahString to sendBytes

not the way you've defined..

EDIT: To answer why your first send_bytes works, and the second doesn't is this: The first one, creates an array of individual bytes. Where as, the second one, creates a string of ascii characteres. So the length of first send_bytes is 13 bytes, where as the length of the second send_bytes is much higher, since the sequence of bytes is ascii equivalent of individual characters in the second blahstring.

Sign up to request clarification or add additional context in comments.

2 Comments

Your code works fine.. however when I change the blahString to blahstring[13] = { cmdstring }; it doesnt work anymore. cmdstring is; char cmdstring ={"\x0B\x11\x00\x02\x00\x69\x85\xA6\x05\x00\x00\x70" }; Any ideas?
you shouldn't do that @AlexvanEs instead just use memcpy() to copy from one string to another. See my answer(see where I use memcpy). Also cmdstring is not a character, its a char *
0

blahstring is a string of characters.

1st character is 0, 2nd character is x, 3rd character is 0, 4th character is B etc. So the line

unsigned char send_bytes[] = { blahstring };

is an array (assuming that you preform a cast!) will have one item.

But the example that works is an array with the 1st character has a value 0x0B, 2nd character is of value 0x11.

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.