Results 1 to 14 of 14

Thread: Strings, integers, and hex oh my!

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189

    Strings, integers, and hex oh my!

    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...
    Code:
    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:
    Code:
    char *run;
    int i; // i can vary from 1-255
    
    run = "\x55\x03\x(i)" // this is not legal
    How can I accomplish this?

  2. #2
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    "\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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189
    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!

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189

    Unhappy

    No go, this is my code:
    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:
    Code:
    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.

  5. #5
    amac
    Guest
    Use single quotes instead of double...

    Code:
        char szTry[5] = "";
    
        szTry[0] = '\x55';
        szTry[1] = '\x05';
        szTry[2] = '\x10';

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    And to have a varying value:
    Code:
    char szTry[5] = {'\x55', '\x03', '\x10', 0, 0};
    int i = 23; // some value
    
    szTry[4] = (char)i;
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189
    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!!

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    what's your exact declaration of szTry?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189
    Well, I just tried this:
    Code:
    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...

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    That's a problem of flash. (The modified version of C)

    Instead of this line:
    char szTry[5] = {'\x55', '\x03', '\x10', 0, 0};

    do:
    Code:
    char szTry[5];
    szTry = {'\x55', '\x03', '\x10', 0, 0};
    This should work (but I'm not sure)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189
    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?

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    szTry[0] = '\x55';
    szTry[1] = '\x03';
    ...


    It seems this thing you have has a really weird behaviour...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189
    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:
    Code:
    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!

    Thanks!

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Aug 2001
    Location
    Minneapolis, MN
    Posts
    189
    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!

    Thanks again all!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width