Convert decimal string to hex string?

13 Apr 2012

Can anyone give a newbie some help converting a decimal string (ie "127") to a string in hex formatted string plus additional characters (ie "!a7F")? Is there a conversion method?

And can anyone give me help on passing string to functions?

13 Apr 2012

This source line: pc.printf("127 decimal (with !a prefix) = !a%x", 127);

produces this output:

127 decimal (with !a prefix) = !a7f

The %x is the format specification that determines the paramter (127) will be displayed in hex.

Joey Ooi wrote:

Can anyone give a newbie some help converting a decimal string (ie "127") to a string in hex formatted string plus additional characters (ie "!a7F")? Is there a conversion method?

And can anyone give me help on passing string to functions?

13 Apr 2012

Joey,

While Chuck's answer about "pc.printf()" is correct, the particular function you will need might be "pc.sprintf()" (the 's' is for results in a 's'tring). Also, the printf() family of functions actually need integers as arguments for the decimal value. So, if your input is really a string, you will need to use "atoi()" as well. You will need a book or website where you can read up on 'standard' C/C++ functions. You will also need to learn which of those functions the mbed on-line compiler has implemented in its libraries.

13 Apr 2012

Thanks. What if I pass a single digit but I want a double digit instead? like convert decimal 10 to string ("!a0A").

And can I ask how to pass that string characters into a function?

ie:

#include "mbed.h"

Serial pc(USBTX, USBRX);


function(string){
 pc.printf("127 decimal (with !a prefix) = !a%x", string);
}

main(){
char string =127;

function(string);
}

13 Apr 2012

Joey,

Something like the following -

#include "mbed.h"

/*
** Do we need an '#include <stdlib>' here to declare standard
** functions sprintf() and atoi()?
*/

Serial pc(USBTX, USBRX); // USB serial channel to host PC

static character outhex[40]; // results go here (large enough?)

static void function(char instring[]){ // private function?

  // only works as expected if string has a proper decimal value -
  sprintf(outhex /* sprintf() puts results in first arg. */,
        "%s decimal (with !a prefix) = !a%02x" /* format string */,
        instring /* unconverted input string as an argument */,
        atoi(instring) /* convert input string to integer argument */
  );
  /* return (void); */
}

void main(void){
static char mystring[] = "127"; // use a string for input

  function(mystring); // create result in 'outhex[]'
  pc.printf("%s\r\n", outhex); // print results to host PC

  while (1) ; // wait forever to ensure printf() can complete

  /* return (void); */ // never reached!
}

The "%02x" means 'left pad with zeros, produce (at least) 2 digits, get value from an integer and format results in hexidecimal (with lowercase letters)'.

I have used char arrays in the above. You may want to use C++ string variables, etc. My example may use more steps/concepts than you require, so adapt as needed.

11 Jun 2012

I'm having a problem like this also I'm receiving values such as 1234 and my code is storing it in a string now when i try to use those values as a string, they come out as much higher! and i have no idea why

Here is my code

#include "mbed.h"
#include "USBMouseKeyboard.h"

USBMouseKeyboard key_mouse;
Serial pc(USBTX, USBRX);

#define NFIELDS (3)
char* pFields[NFIELDS];

void ParseCommands(char* Buffer, char** pFields, uint32_t numFields, char* delimiter)
{
    pc.printf("Command Received\r\n");
    char* pString = Buffer;
    char* pField;
    
    for(uint32_t i=0; i<numFields; i++)
    {
        pField = strtok(pString, delimiter);
        if(pField != NULL)
        {
            pFields[i] = pField;
        }
        else
        {
            pFields[i] = "";
        }
        pString = NULL; //parse next
    }
    
    pc.printf("Command Parsed\r\n");
    if (strcmp("Vector", pFields[0]) == 0)
    {
        pc.printf("Vector Issuing\r\n");
        key_mouse.move(pFields[1],pFields[2]); //Problem with integer?
        pc.printf("Vector Issued\r\n");
    }
    
    if (strcmp("Click", pFields[0]) == 0)
    {
        pc.printf("Click Issuing\r\n");
        //Click code not written yet
        pc.printf("Click Issued\r\n");
    }
    
    if (strcmp("Keyboard", pFields[0]) == 0)
    {
        pc.printf("Keyboard Issuing\r\n");
        //Keyboard code not written yet
        pc.printf("Keyboard Issued\r\n");
    }
}

int main(int argc, char* argv[])
{
    pc.baud(9600);
    char buf[17];
    
    while (1)
    {
        pc.scanf("%s", buf);
        pc.printf("String Received\r\n");
        ParseCommands(buf, pFields, NFIELDS, ",");
    }
}

Now the function "key_mouse.move(string,string);" accepts string input vectors key_mouse.move(pFields[1],pFields[2]); Problem with string?

Even though it is using them directly from a string token! Any idea why this isnt working properly?

By the way: Using the USBDevice Library vector resoltion for x and y is: 32767 so the center of the screen will be X: 16383 Y: 16383

Matt

11 Jun 2012

Matthew,

In the parse function's for() loop - Why should setting 'pString = NULL;' advance to the next field? Shouldn't it be more like 'pString += strlen(pFields[i]);'? Is strtok() supposed to get the address from pString, remember where it's last search ended, and use that when given a NULL pointer?

Is key_mouse.move() supposed to be able to receive string addresses, or do you need to use atoi() as well?

key_mouse.move(atoi(pFields[1]), atoi(pFields[2]));
11 Jun 2012

Fred Scipione wrote:

Matthew,

In the parse function's for() loop - Why should setting 'pString = NULL;' advance to the next field? Shouldn't it be more like 'pString += strlen(pFields[i]);'? Is strtok() supposed to get the address from pString, remember where it's last search ended, and use that when given a NULL pointer?

Is key_mouse.move() supposed to be able to receive string addresses, or do you need to use atoi() as well?

key_mouse.move(atoi(pFields[1]), atoi(pFields[2]));

The NULL Causes it to clear the string for that token which is stored so that it can move onto the next one easily by detecting if it is blank, if the token is blank it is ignored and not added.

The strtok only splits the string into a list of fields which are used to grab the value, its pretty simple.

Check this out: http://mbed.org/compiler/?import=http://mbed.org/users/Elitism/programs/KeyboardMouseSerialTest/mbfof3 Here is the code:

#include "mbed.h"
#include "USBMouseKeyboard.h"

USBMouseKeyboard key_mouse;
Serial pc(USBTX, USBRX);

#define NFIELDS (6)
char* pFields[NFIELDS];

void ParseCommands(char* Buffer, char** pFields, uint32_t numFields, char* delimiter) {
    char* pString = Buffer;
    char* pField;

    for (uint32_t i=0; i<numFields; i++) {
        pField = strtok(pString, delimiter);
        if (pField != NULL) {
            pFields[i] = pField;
        } else {
            pFields[i] = "";
        }
        pString = NULL; //parse next
    }

    if (strcmp("Vector", pFields[0]) == 0) {
        int x;
        int y;
        x = atoi (pFields[1]);
        y = atoi (pFields[2]);
        key_mouse.move(x,y);
    }

    if (strcmp("Click", pFields[0]) == 0) {
        if (strcmp("LPress", pFields[1]) == 0) {
            key_mouse.press(MOUSE_LEFT);
        }
        if (strcmp("LRelease", pFields[1]) == 0) {
            key_mouse.release(MOUSE_LEFT);
        }
        if (strcmp("LClick", pFields[1]) == 0) {
            key_mouse.click(MOUSE_LEFT);
        }
        if (strcmp("RPress", pFields[1]) == 0) {
            key_mouse.press(MOUSE_RIGHT);
        }
        if (strcmp("RRelease", pFields[1]) == 0) {
            key_mouse.release(MOUSE_RIGHT);
        }
        if (strcmp("RClick", pFields[1]) == 0) {
            key_mouse.click(MOUSE_RIGHT);
        }
        pc.printf("Click  Issued\r\n");
    }
    if (strcmp("Keyboard", pFields[0]) == 0) {
        key_mouse.printf(pFields[1]);
        pc.printf("Keyboard Issued\r\n");
    }
}

int main(int argc, char* argv[]) {
    pc.baud(9600);
    char buf[64];

    while (1) {
        //Vector,x,y
        //Click,LClick  //Example: RClick or LPress or LRelease or RPress or RRelease
        //Keyboard,sTrInG //Example: Keyboard,Hello_World //Ignores Spaces and trailing words after a space
        pc.scanf("%s", buf);
        ParseCommands(buf, pFields, NFIELDS, ",");
    }
}

It's mostly complete, the function serial commands are in the Main Event commented out