Howto redirect (convert from hex to dec/Serial.print) fcn output into a var?

LoganPark:
Hello! Very new to Hex/Dec conversion, and I haven't been able to locate the right arduino/C++ tutorial yet.

sprintf may be what you need. You define a "template" of what you want to print, then "sprintf" it to a buffer and print the buffer. For example:

void setup() {
        Serial.begin(9600);
        example1(); // run "conventional" example
        example2(); // run "string in PROGMEM" example
}


void example1(void) // this example wastes ram by putting the mask string in ram
{
        char *buffer; // pointer to buffer
        buffer = (char *) malloc((128) * sizeof(char)); // allocate 128 bytes for a buffer
        if(!buffer) {
                Serial.println("Failed to allocate buffer!");
                return;
        }
        const char *mask = {
                "(conventional)\tHello! My name is %s and I am %d years old which is 0x%02X in hex\r\n"
        };
        sprintf(buffer, mask, "Roger", 55, 55);
        Serial.print(buffer);
        free(buffer); // release memory
}

void example2(void) // this is a better example which stores the mask string in flash memory
{
        char *buffer; // pointer to buffer
        buffer = (char *) malloc((128) * sizeof(char)); // allocate 128 bytes for a buffer
        if(!buffer) {
                Serial.println("Failed to allocate buffer!");
                return;
        }
        const char *mask = {
                PSTR("(PROGMEM)\tHello! My name is %s and I am %d years old which is 0x%02X in hex\r\n")
        };
                sprintf_P(buffer, mask, "Roger", 55, 55);
        Serial.print(buffer);
}

void loop()
{
}

Notice the differences... PSTR("string info") places the string in flash memory and sprint**_F** is used to extract the string from flash memory.

NOTE! sprintf and sscanf do not support FLOATING POINT NUMBERS. If you try to use floating point in your program (for example, mask = { "The temperature is %f degrees\r\n" }, then sprintf(buffer, mask, 79.4); ), it will fail. The temperature will display as a question mark! The result would be "The temperature is ? degrees" instead of "The temperature is 79.4 degrees".

The people who wrote the Arduino code felt that leaving out floating point support was good because it uses about 1.5K more code (i.e. your sketches end up about 1.5K larger). But if you want full floating point support for sprintf and sscanf, use this script to modify your libc.a files (it removes the crippled functions and replaces them with the full functions). Note though that your sketches will be about 1.5K larger if you do this.

Script "fixfp" - installs floating point support

#!/bin/bash
################################################################################
# fixfp - script to install floating point support into Arduino printf library
#
# For more information, see this post:
# http://arduino.cc/forum/index.php/topic,124809.msg938573.html#msg938573
################################################################################

STATUS=0

## Exit if libc.a isn't here
test -e libc.a
if [ ${?} -ne 0 ]; then {
        echo "File 'libc.a' not found - exiting"
        exit 0
} fi

test -e libc.a.orig
let STATUS+=${?}

test -e vfprintf_flt.o
let STATUS+=${?}

test -e vfscanf_flt.o
let STATUS+=${?}

if [ $STATUS -eq 0 ]; then {
        echo "Floating point patch already performed - exiting"
        exit 0
} else {
        cp libc.a libc.a.orig
        ar -dv libc.a vfprintf_std.o
        ar -dv libc.a vfscanf_std.o
        ar -xv libprintf_flt.a vfprintf_flt.o
        ar -xv libscanf_flt.a vfscanf_flt.o
        ar -rv libc.a vfprintf_flt.o
        ar -rv libc.a vfscanf_flt.o
        echo "Floating point patch installed."
} fi

Change to the directory that contains your libc.a files (typically "arduino-1.0.1/hardware/tools/avr/lib/avr/lib"), then run the script. In addition to fixing the floating point code, it will also make backups of the original libraries so you can go back if you wish.

If you use Windows cough then you will need to find where your libraries are stored and modify the script to be a batch file. I have no idea how to do that (maybe someone else can chime in and tell you?).

Hope this all helps.

-- Roger