Click to See Complete Forum and Search --> : Strings, integers, and hex oh my!
Kraig K
Mar 4th, 2002, 01:23 PM
I have an imbedded controller (Rabbit 2000) that is programmed in a modified form of C, but I think much of the "basics" are the same. I have to send commands to a device in hex. The following...
char *run;
run = "\x55\x03\x10";
serBputs(run); //sends data out the serial port
...starts my device. But I want to be able to vary the last value. I've tried several things, creating another string without the last value, setting an integer to say "100", converting that to a string and combining them. This works on a fundamental level, but the "100" doesn't get converted to hex, it thinks it's a string.
So what I want is:
char *run;
int i; // i can vary from 1-255
run = "\x55\x03\x(i)" // this is not legal
How can I accomplish this?
riis
Mar 4th, 2002, 02:01 PM
"\x55\x03\x10" is a string of 3 chars (and the 0-char at position 3 of course). All chars are hexadecimal.
If you need to modify the last char (in this case the "\x10"), you can change it like this: run[2] = (whatever char you like).
However, the "100" in your example won't work (if hexadecimal), since it's just too large. The range is \x00 to \xFF (which is equal to 0 - 255). But if you mean the decimal 100, you can use it. It's represented as \x64.
Kraig K
Mar 4th, 2002, 03:33 PM
I haven't tried it yet but it sounds good. One thing though, the "\x" converts the decimal to hex... So are you saying that the "Run[2] = " statement won't convert it?? I need an integer to be what I replace with... In other words, I need to be able to use the full 0-255 range in that last position. I'll play around with it now, maybe I'll be enlightened! :D
Kraig K
Mar 4th, 2002, 04:02 PM
No go, this is my code:
char try[5];
main() {
try[0] = "\x55";
try[1] = "\x03";
try[2] = "\x10";
}
It won't even assign the array. The error I've been seeing alot of is: "Type Mismatch: assigning a value of type 'char' to type 'char[]'"
I've also tried:
char *try;
char try[];
try = "\x55\x03\x10";
And it won't compile. I also tried putting the char assignment in the "main" and it doesn't like that either.
amac
Mar 4th, 2002, 07:25 PM
Use single quotes instead of double...
char szTry[5] = "";
szTry[0] = '\x55';
szTry[1] = '\x05';
szTry[2] = '\x10';
CornedBee
Mar 5th, 2002, 06:50 AM
And to have a varying value:
char szTry[5] = {'\x55', '\x03', '\x10', 0, 0};
int i = 23; // some value
szTry[4] = (char)i;
Kraig K
Mar 5th, 2002, 08:20 AM
It's almost working. But when I try to set one of the characters using szTry[2] = something It gives me "assignment to read-only variable not allowed".
Thanks for all the help thus far!!
CornedBee
Mar 5th, 2002, 11:45 AM
what's your exact declaration of szTry?
Kraig K
Mar 5th, 2002, 12:06 PM
Well, I just tried this:
main() {
char szTry[5] = {'\x55', '\x03', '\x10', 0, 0};
int i = 23; // some value
szTry[4] = (char)i;
printf("MSG: \"%s\"\n\n", szTry);
}
Before I had alot of other code in my program, but I trimmed it down. I also tried putting the declaration before main. I also am getting a warning that may be specific to my microprocessor and may be part of that problem?? It says: "Initialized variables are placed in flash as constants. Use keyword 'const' to remove this warning." But it's only a warning, it errors on the assignment to read-only...
CornedBee
Mar 6th, 2002, 09:04 AM
That's a problem of flash. (The modified version of C)
Instead of this line:
char szTry[5] = {'\x55', '\x03', '\x10', 0, 0};
do:
char szTry[5];
szTry = {'\x55', '\x03', '\x10', 0, 0};
This should work (but I'm not sure)
Kraig K
Mar 7th, 2002, 07:32 AM
Bad news, the compiler really doesn't like" szTry = {'\x55', '\x03', '\x10', 0, 0}; It gives a slew of errors:
"Invalid expression - need left hand value"
"Missing ";" - there are several of these
"\x10 out of scope/not declared"
It only displays ten errors... I tried changing several things, the "," to ";", tried using just normal characters instead, nothing got me any further.
Any other ideas?
CornedBee
Mar 7th, 2002, 11:11 AM
szTry[0] = '\x55';
szTry[1] = '\x03';
...
It seems this thing you have has a really weird behaviour...
Kraig K
Mar 7th, 2002, 11:29 AM
Thanks for your persistence CornedBee. Your last suggestion is very close to working, it conpiles and runs, but I get too many characters. I may be able to fix that, I just don't have time right now. So this is what I have:
main() {
char szTry[3];
int i;
i = 10;
szTry[0] = '\x55';
szTry[1] = '\x03';
szTry[2] = '\x10';
printf("MSG: \"%s\"\n\n", szTry);
szTry[2] = (char)i;
printf("MSG: \"%s\"\n\n", szTry);
}
I think all your suggestions didn't work b/c of the modified C version I have. Are you familiar with the Rabbit processor and Dynamic C? Here's their website: www.zworld.com/products/dc/index.html
They have a support forum too, I posted this question there this morning, but I came here first b/c their forum is slow and the biggest reason, smart ppl hang out here! :D
Thanks!
Kraig K
Mar 7th, 2002, 11:53 AM
Just an update. The extra characters are ignored by my device, so IT WORKS! I think I was close before with amac's idea, but I didn't heed riis' comment about hex values. When I was trying to use 100, that was too big for hex.
Who knows when you're debugging, you try so many things that you forget what you did and didn't do! :rolleyes:
Thanks again all! :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.