CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

int40 to string
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Eddy71ua



Joined: 23 Sep 2009
Posts: 55
Location: Ukraine

View user's profile Send private message Visit poster's website

int40 to string
PostPosted: Sat Jun 17, 2023 6:54 am     Reply with quote

Can you please tell me how to convert an 40-bit unsigned integer to a string?
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Sat Jun 17, 2023 6:58 am     Reply with quote

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: 471
Location: Montenegro

View user's profile Send private message

PostPosted: Sat Jun 17, 2023 8:04 am     Reply with quote

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: 19477

View user's profile Send private message

PostPosted: Sat Jun 17, 2023 9:39 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sat Jun 17, 2023 11:46 pm     Reply with quote

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: 471
Location: Montenegro

View user's profile Send private message

PostPosted: Sun Jun 18, 2023 3:22 am     Reply with quote

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: 9215
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Jun 18, 2023 4:50 am     Reply with quote

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: 19477

View user's profile Send private message

PostPosted: Sun Jun 18, 2023 6:44 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Mon Jun 19, 2023 2:43 am     Reply with quote

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: 19477

View user's profile Send private message

PostPosted: Mon Jun 19, 2023 3:37 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Mon Jun 19, 2023 4:39 am     Reply with quote

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: 19477

View user's profile Send private message

PostPosted: Mon Jun 19, 2023 6:17 am     Reply with quote

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: 471
Location: Montenegro

View user's profile Send private message

PostPosted: Mon Jun 19, 2023 7:47 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Mon Jun 19, 2023 7:56 am     Reply with quote

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: 9215
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Jun 19, 2023 6:44 pm     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group