Conversion of int?

Hi,

I am using some arduino code I found to communicate with something on a remote pc. The general idea is that I send a number (between 0-400) to the remote pc.

I found a sample but I am not sure what it is doing. I try replacing it so it send the int, but it complains as it says it requires a char *

//variable temp contains my int with the number between 0 and 400
//this is from the code itself
char msgBuf[40]; // What does this do?
sprintf(msgBuf, "{%d}", temp);  //on the other side I get this (example: {251} ) 
Serial.println(msgBuf); //this tends to give values of 150-300 when

//send message
sendData(msgBuf);

the sendData function:

void sendData( char *data) {
    //Bunch of irrelevant lines here
    // Write data as hex characters
    char *ptr = data;
    int idiotCount = 50;
    while (*ptr && idiotCount ) {
        Serial.write( getHexHi( *ptr ) );
        Serial.write( getHexLo( *ptr ) );

        ptr++;
        idiotCount--;
    }
    Serial.write("\n");
    delay(5000);

    //more irrelevant code
}

The functions called by that:

char getHexHi( char ch ) {
    char nibble = ch >> 4;
    return (nibble > 9) ? nibble + 'A' - 10 : nibble + '0';
}
char getHexLo( char ch ) {
    char nibble = ch & 0x0f;
    return (nibble > 9) ? nibble + 'A' - 10 : nibble + '0';
}

My question is: What does it convert the number into, and how do I later convert it back into the original number?

If you could please explain it in a very simple way. Thanks for your time!

It converts it to ASCII so we people can read it.

But to be honest, it a XY-problem. Tell us what you really want to do. What kind of value? What's on the PC that tries to read it etc.

I want to send a number (between 0-400). So I give it that number as "temp".

In the computer I am making a graph out of that data. Unfortunately instead of having a graph that shows 3 when temp is 3, it shows another much higher value (something like 200ish).

The number on the Arduino part, okay, fine.

The PC part, okay, want to make a graph. So you try/fail to use some software. Is it that hard to damn tell us what you try to use?

I am using python. So on the pc I am getting the following

{251}{“time”:“2016-03-04T11:07:46.022Z”}
{104}{“time”:“2016-03-04T11:07:30.016Z”}
{187}{“time”:“2016-03-04T11:07:14.008Z”}

(and so on)

which is the same as msgBuf.

On python I am doing the following:

import datetime
import json
import time
import urllib.request
import pygal
from pygal.style import DarkSolarizedStyle
import re

# a bunch of unrelated code

def load_data():
    page = *** # my url, removed it, because posting on the internet

    with urllib.request.urlopen(page) as url:
        s = url.read().decode('utf-8')
    data = json.loads(s)

    temp = []
    for i in range(len(data)):
        temp.append([data[i][u'time'],data[i][u'data_plain']])

    return temp

# a bunch of unrelated code

result = load_data()
    times = []
    dist = []

    # Clean date and measurement
    for i in result:
        # 2016-03-01T12:42:04.285Z
        d = datetime.datetime.strptime(i[0], "%Y-%m-%dT%H:%M:%S.%fZ" )
        t = time.mktime(d.timetuple()) + d.microsecond / 1E6
        times.append(int(t))
        r = re.sub('[^0-9]','', i[1])
        dist.append(int(r))

    #Graph
    title = "Values for " +  str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(times[-1]))) + " to " + str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(times[0])))
    bar_chart = pygal.Bar(width=1200, height=600,
                          explicit_size=True, title=title,
                          style=DarkSolarizedStyle,
                          disable_xml_declaration=True)
    bar_chart.x_labels = times
    bar_chart.add('Milimeters', dist)

    return render_template('test.html', data=result, bar_chart=bar_chart)

sorry if I misunderstood your question earlier

Anyone that can help me in how to reverse the number back to normal?

Then I think it's more of a Python question...

The Arduino code you posted formats it in a very specific way. If you just want to transfer a number there is no need for all the overhead which makes it all complicated. Just sending the value is as simple as

Serial.println(value);