Char to uint_32 t

Hi there!

looking for a way to convert char i get from a device via I2C tu uint_32_t

Data fetched from device by Wire.h. as String (not changeable)

Example format: $bladibladibla,3219.0000,nextbladibladibla#

Data is fetched by string.h at semicolons and the dot is removed.

Example string offered as Char: 32190000 (Still O.K.)

Data may vary but consists only of Numbers.

I need that data as uint_32_t

No clue how to do that..

Thanks for help!

Data fetched from device by Wire.h. as String (not changeable)

String is a very poor choice. Why are you boxing yourself in this way?

Data is fetched by string.h

The string.h is a header file. It defines methods that do stuff. It, itself, doesn't fetch anything, and certainly does NOT fetch stuff as String objects.

at semicolons and the dot is removed.

What does "fetched at semicolons" mean? Are you parsing the string, using semicolons as delimiters?

Example string offered as Char: 32190000 (Still O.K.)

There is no such thing as Char. There is char and char array.

If you have a string (a NULL terminated array of char), as opposed to a String, you can use atol() to convert that value to a long, which is the same size as a uint32_t (there is only one underscore). If the value is positive, there will be no loss of integrity storing the signed long that strtol() produces in a uint32_t variable.

uint_32_t long_result;

long_result = byte1; // bring in Most signicant byte
long_result = long_result<<8; // shift it left
long_result = long_result + byte2; // bring in next MSB
long_result = long_result<<8; // shft it left
long_result = long_result + byte3 // bring in next MSB
long_result = long_result<<8; // shift it left
long_result = long_result + byte4; // bring in LSB

probably more elegant ways to write this up, but this will work.

but this will work.

Not to convert characters, it won't.

You have a string in decimal so you have to find the decimal point and multiply each character by 10 to the power of it's position when it is converted to a number.
You can convert a char into a number with a & 0xf (mask out all but the last nibble)

O.K. - now that every face is slapped:

What about a Solution ?

#include <stdio.h>
#include <wire.h>
#include <string.h>
#include <config.h>

...
..
static int32_t  GPS_latitude,GPS_longitude;
static uint8_t  GPS_fix , GPS_fix_home = 0;
static uint8_t  GPS_numSat;

And then later we get started:

void parseString (char *str)
{
  /*char str[] = "$GPGGA,091926.000,3113.3166,N,12121.2682,E,1,09,0.9,36.9,M,7.9,,0000*56<CR><LF>"*/   /*Testcase*/
  char * pch; /*const int numberOfElements = 14;*/

int i = 0;/*printf ("Splitting string \"%s\" into tokens:\n",str);*/
  pch = strtok (str,","); 
  while (pch != NULL)
  {
i++;
    printf ("%s\n",pch);

switch (i) {
case 1:
GPS_start_time = pch; /*  typecast mMn ?*/
break;
case 2:
GPS_latitude = pch; /* typecast mMn ?*/
break;
case 4:
GPS_longitude = pch; /* typecast mMn ?*/
break;
case 6:
GPS_fix = pch; /* may */
break;
case 7:
GPS_numSat = pch; /* maybe a 0, could make problems*/
break;
default:
break;
}
    pch = strtok (NULL, ",");/* here we ignore the rest of the NMEA-String*/

if i > 7 {

break;

...
..
.
parseString(Wire.beginTransmission(GPS_I2C_ADDRESS))

Quite incoplete but as overview.
I am just a 6-weeker in c now..patience , please

So, you are using char array (string, not String). You are getting a NULL terminated array of characters from the string using strtok(). Perfect. Just pass pch to strtol to get a long.

static int32_t  GPS_latitude,GPS_longitude;
static uint8_t  GPS_fix , GPS_fix_home = 0;
static uint8_t  GPS_numSat;

Are these global variables? They should be global or static - not both.

The variables i want to feed with my build.
Won´t touch them .
Works and a lot of other stuff i don`t understand depends on em .

Just pass pch to strtol <- small codehint please- will do the repetitive stuff later... :blush:

Back in 5 Mins...will fetch the "thick Book " and read it up meanwhile.For shure ist is described in a complicted manner .. for anybody never to get a clue from..

Same stuff in VBa - quite simple but any book i fetched makes a huge problem out of nothing now looking at them already able to do complicated stuff with it.

Just pass pch to strtol <- small codehint please- will do the repetitive stuff later...

Ok. Just this once:

long val = strtol(pch);

Thanks!

Will have to read up a lot on this - soooner or later i will need more of this.

#include <stdlib.h>

As mentioned: book makes it complicated .
Human readable explanation found in BSD manfiles.

Quite a lot Work left...

Jesus , c is big.... :astonished:

c is big

Wait until you dive into C++. Makes c look a puddle next to an ocean.

PaulS:

Just pass pch to strtol <- small codehint please- will do the repetitive stuff later...

Ok. Just this once:

long val = strtol(pch);

either you meant to use atol there (which should work as well), or you're missing a couple parameters in that strtol call.