View previous topic :: View next topic |
Author |
Message |
Eddy71ua
Joined: 23 Sep 2009 Posts: 55 Location: Ukraine
|
int40 to string |
Posted: Sat Jun 17, 2023 6:54 am |
|
|
Can you please tell me how to convert an 40-bit unsigned integer to a string? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Sat Jun 17, 2023 6:58 am |
|
|
You would have to store it in something like an int8 array, so just print
the bytes in this in whatever format you want. Presumably as hex?. |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 480 Location: Montenegro
|
|
Posted: Sat Jun 17, 2023 8:04 am |
|
|
Out of curiosity: how do you declare an int40? CCS has an example in help of how to convert a number to a string. I'm sure it works for other than floats.
Quote: |
The following is an example of how to direct the output of a printf to a string. We used the \f to indicate the start of the string.
This example shows how to put a floating point number in a string.
main() {
char string[20];
float f;
f=12.345;
sprintf(string,"\f%6.3f",f);
|
My mind works like this: I'd split the 40bit number to 3 16 bit numbers, print them to three strings and join them. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Sat Jun 17, 2023 9:39 am |
|
|
I assumed this is arriving from something else. So just data in an array
on the PIC.
He doesn't tell us what PIC?. If is is a DSPic, then he can simply write
it into an int64, and print this directly.
If it is a data array from an external device, then as I said, the easiest
way to print it in hex is byte by byte.
The difficult one would be if he wants to print it in decimal. He would have
to write his own int40 arithmetic rouines to do this. |
|
|
Eddy71ua
Joined: 23 Sep 2009 Posts: 55 Location: Ukraine
|
|
Posted: Sat Jun 17, 2023 11:46 pm |
|
|
I use a PIC18 to make a frequency meter. I get the data from the assembler insert in the program as a set of 5 bytes.
I want to convert this number to BCD-format for further display on the screen. |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 480 Location: Montenegro
|
|
Posted: Sun Jun 18, 2023 3:22 am |
|
|
2 to the power of 40 is 1.099.511.627.776 or roughly 10 to the power of twelve. I don't know how to pronounce that number, but I'm quite sure no PIC can measure frequencies that high :-). Could you give maybe two examples of your data and what is expected on the screen?
Last edited by PrinceNai on Sun Jun 18, 2023 7:25 am; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Sun Jun 18, 2023 4:50 am |
|
|
I am curious about the 'assembler insert' code as well. Can you post a 'link' to it ? Maybe it's a LOT easier to convert than you think !!! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Sun Jun 18, 2023 6:44 am |
|
|
It is possible that is is a 10 GHz counter, so could have perhaps 34bits
of useable count.
It would be useful though to know how this is actually being presented to
the processor?. I must admit I do find myself wondering if it is actually
using BCD counters, so though 40bits are received, this is only actually
ten digits. In which case the data may already be 'in' BCD format, so
no conversion at all is needed!... |
|
|
Eddy71ua
Joined: 23 Sep 2009 Posts: 55 Location: Ukraine
|
|
Posted: Mon Jun 19, 2023 2:43 am |
|
|
The program has a regular binary counter that accumulates data from the timer + adds a timer overflow and then does the averaging.
From int32 I did the conversion earlier through the itoa32() function.
But there are five bytes here, not four... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Mon Jun 19, 2023 3:37 am |
|
|
You still haven't said what PIC?.
As already pointed out, 2^40, is beyond any really likely count.
However to just output in hex, simple per byte code. |
|
|
Eddy71ua
Joined: 23 Sep 2009 Posts: 55 Location: Ukraine
|
|
Posted: Mon Jun 19, 2023 4:39 am |
|
|
Sorry, PIC18F26k22
Now I'm thinking of limiting the input data to int32 and converting to float32, doing the math with float format |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Mon Jun 19, 2023 6:17 am |
|
|
Problem with that is you are losing a huge amount of resolution.
Remember a float32 only gives 23bits of real precision.
Could you switch to a DsPIC?. If so, these have an int64 type.
Alternatively, I have in he past used BCD arithmetic libraries, which can
work well, and support large sizes. GitHub has a C library that translates
fairly easily. |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 480 Location: Montenegro
|
|
Posted: Mon Jun 19, 2023 7:47 am |
|
|
It would be really nice if you could post an example 40bit number and the final string from that.
Last edited by PrinceNai on Mon Jun 19, 2023 8:28 am; edited 2 times in total |
|
|
Eddy71ua
Joined: 23 Sep 2009 Posts: 55 Location: Ukraine
|
|
Posted: Mon Jun 19, 2023 7:56 am |
|
|
I also thought about the loss of accuracy of int to float conversion today. I don't want to lose significant digits.
On the piclist.com site I found an assembly conversion from binary code to bcd. I will try to apply it in the program. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Mon Jun 19, 2023 6:44 pm |
|
|
instead of floats, use 'scaled integers'...
somehow....
still trying to figure out that 40 bits is a HUGE number...yeesh.
I've seen some counter where each bit is 1us, makes the 'conversion' a lot easier. |
|
|
|