Hello.
I have a problem with the digit that I get from operating as a float variable
example:
digit = 18639864.00
I would like to divide this digit into parts
A = first two digits (18)
B = second two digits (63)
C = third two digits (98)
D = fourth two digits (64)
Maybe someone knows some simple code to divide this number according to the example above.
I'll give you a hint - if you divided it (as an integer) by 100, what would the remainder be?
Why it is float? It looks like it could be integer.
If I were nitpicking, I would ask what is a "fast digit"?
If I were being picky, I would ask why "digit" seems to consist of ten digits.
Can anyone paste some sample code on how to do this.
I'd start from the point of view of it not being any sort of floating point number, otherwise things might not turn out as you'd want.
This sounds awfully like an XY problem
Damian01:
Can anyone paste some sample code on how to do this.
How about responding to reply #1 first?
I was able to convert the number to int32_t
uint32_t test;
now shows me:
test=18639864
maybe now it will be easier to divide it into parts.
Or maybe there is some way to display data from 3 digits without the first two
Try to test what the result of "number % 100" and "number / 100" could be..
Damian01:
I was able to convert the number to int32_t
I'm curious - what did you convert it from?
Danois90 thank you very much for the hint I used what you gave and it works: D
Given:
unsigned long test = 18639864;
1. Using >> (right shift) and & (AND) Operators
==> unsigned long = 0x ; //0x is inserted to keep the image of the number
byte D0D1 = (byte) test >> 24; //D0D1 = 18
byte D2D3 = (byte) (test >> 16) & 0x000000FF; //D2D3 = 63
byte D4D5 = (byte) (test >> 8) & 0x000000FF; //D4D5 = 98
byte D6D7 = (byte) test & 0x000000FF; //D6D7 = 64
2. Using % (modulus) and / (division) Operators
==> unslgned long test = 18639864;
==> unsigned long test = ;
byte indexdd[4];
for(int i=0; i<4; i++)
{
indexdd[i] = test%100; //indexdd[0] = 64, indexdd1[1] = 98, indexdd[2] = 63, indexdd[3] = 18
test = test/100;
}
GolamMostafa:
1. Using >> (right shift) and & (AND) Operators
Cannot see how that's gonna work with base 10 integers..
Danois90:
Cannot see how that's gonna work with base 10 integers..
The trick is the insertion of preamble 0x with the number 18639864 in order to keep an intact image of the given number in the memory.
void setup()
{
Serial.begin(9600);
unsigned long test = 0x18639864;
byte D0D1 = (byte)(test>>24) & 0x000000FF; //D0D1 = 18
byte D2D3 = (byte) (test >> 16) & 0x000000FF; //D2D3 = 63
byte D4D5 = (byte) (test >> 8) & 0x000000FF; //D4D5 = 98
byte D6D7 = (byte) test & 0x000000FF; //D6D7 = 64
//----------------------------------
Serial.println(D0D1, HEX); //shows: 18
Serial.println(D2D3, HEX); //shows: 63
Serial.println(D4D5, HEX); //showss: 98
Serial.println(D6D7, HEX); //shows: 64
}
void loop()
{
}
But now I have a different problem
It turned out that I need clean data extracted from GPS.
Exactly from a ruler.
$GNGGA,091609.00,5020.37459,N,01838.41470,E,2,08,0.95,257.5,M,40.4,M,,0000*48
I need to get these numbers from this line without using the TinyGPS library
Lat = 5020.37459
Lon = 01838.41470
GolamMostafa:
The trick is the insertion of preamble 0x with the number 18639864
It still does not make any sense.. Given a base 10 integer read dynamically into a variable, how would that work?
Damian01:
But now I have a different problem
It turned out that I need clean data extracted from GPS.
Exactly from a ruler.
$GNGGA,091609.00,5020.37459,N,01838.41470,E,2,08,0.95,257.5,M,40.4,M,,0000*48
I need to get these numbers from this line without using the TinyGPS library
Lat = 5020.37459
Lon = 01838.41470
Look at strtok, it will allow you to extract the numbers from the string.
GolamMostafa:
The trick is the insertion of preamble 0x with the number 18639864 in order to keep an intact image of the given number in the memory.
void setup()
{
Serial.begin(9600);
unsigned long test = 0x18639864;
That's not the same value. It only looks similar since it has the same digits, but it's in a different base. The question was not how to break down a hexidecimal value.