??? 11/04/08 08:25 Read: times |
#159614 - Bandwidth of facts Responding to: ???'s previous message |
But you still came up with the "wrong" solution because you did not read all information about sprintf(), and all information you have received in this thread.
The standard C definition of sprintf() is that you use: sprintf(buf,"%02x%02x%02x",10,20,30);to print values as two hexadecimal characters, with some embedded compilers requiring a special attribute byte to separate between 8-bit and 16-bit constants and between data stored in code or data space. This is how you use sprintf() to produce hexadecimal output. But you do not want hexadecimal output. You want binary output. The correct way to get a character or (small integer) to be converted into binary output is %c, as you figured out. But you need to be very careful since you may not print the binary value \x00 since that will lead to a remature end of the string - unless you keep track of the real size of the string yourself. But you seem to have missed one thing noted earlier in this thread: sprintf(buf,"\x0a\x0b\x0c"); strcpy(buf,"\x0a\x0b\x0c"); strcat(buf,"\x0a\x0b\x0c"); ... So your code: sprintf( ComString, "%c%c%c%c%c", 0x02,0x20,0x2E,0x2E,0x0D );is consuming a lot of code space by treating your constants 0x02, 0x20, 0x2e, ... as if they where variables, and requesting formatted printing of them. The correct concept - if size/efficiency/speed matters - would be something like: sprintf(CmdString,"\x02\x20\x2e%c\x2E\x0D",eight_bit_variable);if you want to have a single variable byte in the middle of a string. If you only want constant data, then you don't need spritnf() at all since you don't need any "formatted output" but just constant output. In processors where the RAM isn't so limited, you normally do define the full message as: #define NELEM(a) (sizeof(a)/sizeof(*(a))) unsigned char command_x[] = { 0x10,0x20,0x30,0x40,0,0x50,0x60 };And then you keep track of the offset of you reserved byte that should be replaced with a variable: command_x[4] = my_eight_bit_variable; send_command(command_x,NELEM(command_x)); You would be greatly helped by getting a good book on C, and read it once or twice. It will not just give you the answer to the questions you have now. It would also give you the answer to the questions you didn't knew that you had. The problem with knowledge, is that it is hard to know how much information you do not know. And when you do not know that you miss information, then you can't write questions asking for that information. It takes knowledge to ask questions. Spending time with a general-purpose book on the C language, and spending time reading the manual for the different runtime-library functions will give you that knowledge. And it will give you that information way faster than you can gain it by asking questions on forums. You think that you get answers quickly here on this forum? But the problem is that every answer you get only supplies a very minimal amount of real facts, since the answers you get are only related to the questions you ask. The forum responses will not cover tha questions you forgot to ask because you didn't know that you should have asked or because you (incorrectly) thought you already knew the answer. That is why I wrote as I did. A forum is excellent for helping out when you get stuck, by giving you a second take on a problem, but there are way better ways to collect general knowledge. Google has way better bandwidth - anything related to the C language can already be found with Google. With some creativity, you could even find copies of the full C language stanard, even if I don't recommend reading the standard until you have a reasonable grasp of the language. You count efficiency in the turn-around time on a forum. I count efficiency in the total bandwidth of relevant facts I can get access to. A forum may be able to peak at a couple of characters / second. Google can give you megabytes/second (if your Internet line can support it) with relevant information. So learning to master Google is your best investment. And if you happen to hate Google, then find another search engine that you are comfortable with. But learn how to use one. I can almost always result in faster answers to problems. |
Topic | Author | Date |
Hex byte stream | 01/01/70 00:00 | |
Declare an initialised array | 01/01/70 00:00 | |
why not in code memory??? | 01/01/70 00:00 | |
yes, if... | 01/01/70 00:00 | |
Initialized array | 01/01/70 00:00 | |
memcpy | 01/01/70 00:00 | |
Thanks. | 01/01/70 00:00 | |
You can build your array dynamically | 01/01/70 00:00 | |
sprintf ?? | 01/01/70 00:00 | |
Manuals? | 01/01/70 00:00 | |
Perception | 01/01/70 00:00 | |
Bandwidth of facts | 01/01/70 00:00 | |
You will make a good master !![]() | 01/01/70 00:00 |