News:

Let's find out together what makes a PIC Tick!

Main Menu

Dword to String converter

Started by JackB, Dec 11, 2024, 09:30 PM

Previous topic - Next topic

JackB

Hello everyone

When looking for a Dword to String converter I've read about the compiler 8 manual page:399
then I took sometime to read about different function from Positron and came up with
this way to convert a Dword value to a string, value tested range from 0~4294967295.
the code listed is free to use as is enjoy.

Device = 18F14K22       
Declare Xtal = 64
Config_Start
  FOSC = IRC    ; Internal RC oscillator
  PLLEN = On    ; PLL is under software control
  MCLRE = OFF
Config_End
OSCCON = $70    ;for 64Mhz   ;p:17
Declare Hserial_Baud = 9600
Declare HRSOut1_Pin = PORTB.7

MAIN:
While PORTA.3 = 1: Wend             ' Button Press
While PORTA.3 = 0: Wend             ' Start on release
DelayMS 20                          ' Tactile switch Bounce delay

Dim ABC As Dword                    ' ABC Dword
Dim PDigit As Byte                  ' Dig / Digit
Dim ABCString As String = ""        ' ABC String
Dim I As Byte                       ' Extract digit position
ABC = 4294967295                    ' Dword Value

For I = 9 To 0 Step -1              ' Start Loop to 0
    PDigit = Dig ABC, I             ' Extract digit's position
    Inc PDigit, 48                  ' Convert to ASCII (0~9)
    ABCString = ABCString + PDigit  ' String concatenation
Next

HRSOutLn  ABCString                 ' Send to Terminal
GoTo MAIN

trastikata

:)
sMyString = Str$(Dec dMyDword)

JackB

Thx,
for the ASCII Decimal converter


JackB


Hello,

I just found the example code in Positron manual

(Strn String1 = Str$(Dec Wrd1) ' Convert the Integer to a String)
work even better with less code.

Thank you again for your help.


top204

With a String variable, there is no need to use the Strn command, just use the String's name:

String1 = Str$(Dec Wrd1)

The Strn command was added for the old, standard 14-bit core devices many years ago, before I added real String variables to the compiler for 18F devices, then for the enhanced 14-bit core devices when they came out, because they both have a linear RAM mechanism for indirect access to it. So the Strn command uses a byte array as well, and adds a terminating null to the end of it.

JackB

Many thanks Les,

You took the time to explain clearly the 'under the hood' process for theses function

this is a great way to help novice like me to understand.

If I may ask is there an new version of the compiler book in the future, or this is the last edition.

Thanks again for the great work you do.

RGV250

QuoteIf I may ask is there an new version of the compiler book in the future, or this is the last edition.
Do you mean the manual, if so it is constantly updated when new features are added or a typo etc is discovered.

Regards,
Bob

JackB


The 'Positron8 BASIC Compiler Manual'
since there's no revision date printed on the manual.
like on page 51 found this where [Index] should be  [bIndex]
'bMyArray[Index] = bIndex * 10              ' Write Index*10 to each element of the array.'

Device = 16F88 ' We                         'll use a smaller device this time
Declare Xtal = 4                            ' Tell the compiler the device will be operating at 4MHz
Declare Hserial_Baud = 9600
Dim bMyArray[10] as Byte                    ' Create a 10 element Byte array.
Dim bIndex as Byte                          ' Create a Byte variable.

For bIndex = 0 to 9                         ' Repeat with bIndex= 0,1,2...9
 bMyArray[Index] = bIndex * 10              ' Write Index*10 to each element of the array.
 Next
 For bIndex = 0 to 9                        ' Repeat with Index= 0,1,2...9
 HRsoutLn Dec bMyArray [bIndex]             ' Show the contents of each element.
 DelayMs 500                                ' Wait long enough to view the values
Next



RGV250

#8
Hi,
That's the sort of think now it has been flagged up will probably be updated in the next update, it is correct in the example on the next page.
As far as I am aware the manual gets updated when the compiler is updated so should be current.
Don't forget, Les is the only person updating the compiler (and doing an excellent job of it) and cannot proof read almost 1000 pages(8 and 16 bit manuals), update the compiler and make a living at the same time.



Bob

John Lawton

Quote from: JackB on Dec 12, 2024, 07:24 PMThe 'Positron8 BASIC Compiler Manual'
since there's no revision date printed on the manual.

Au contaire, in my 8 bit manual which I believe is the latest, on page 2 it states:
"Revision 4.0.4.6 of the Manual." I assume this corresponds to the compiler version when the manual was last updated and the date of that version is held in the "Whats New.htm" file.

It used to be the case that there was no versioning in the manual but Les listened and has added it.

John


JackB

Thanks Bob, & John for pointing to source materials

found the document ""C:\Program Files (x86)\ProtonIDE\PDS\Docs\Whats New.htm""

from the beginning I used the manual online "https://sites.google.com/view/rosetta-tech/home"
and I'de like to point also by leaving a message about the code on page 51, was to let someone
know for the future manual update.
For many times Les, helped me a lot by taking the time to explain the "under the hood" different function
related to my inquiry.

Again I'd like to say that Les, interaction with the community on this forum is'Excellent' no doubt about it.

RGV250

Hi Jack,
Just in case you did not know, the manuals and Whats New can be found by going to Help / Documents in Positron.

Bob

trastikata

The current manual is easily accessible via the IDE: Help-Documents-Positron 8 / 16 Manual

Manual.jpg

Mitch

Hi,

I am using a DWord to hold a 5 digit serial number. I convert the ASCII string to a DWord and store in code space.

If I send it back to the screen it has been stored correctly. For example storing 54321 and then reading it back to DWord variable Serial_Number and then printing it with:

HBusOut LCD, [Dec5 Serial_Number], Gives the correct number of 54321.

However, if I use either the command:

StrN Output_String = Str$ (Dec5 Serial_Number), or
Output_String = Str$ (Dec5 Serial_Number)

and then print the contents of the string with:

HBusOut LCD, [Str Output_String \5], I get 54272.

Alternatively using:

For Index = 0 To 4
   Data_Out = Output_String [Index]
   HBusOut LCD, [Data_Out]
Next Index

Gives exactly the same output of 54272

I have tried many permutations of code syntax but always get the same result.

So I cannot see why the standard string conversion command does not give the correct result.

Has anyone had issues with this sort of conversion?

Thanks

RGV250

Hi,
I think it would have been better if you had started a new post for this.

What happens if you send it to a serial port?

Regards,
Bob

Mitch

Hi Bob,

Thanks for responding. Sending it to the serial port does exactly the same.

Should I repost this?

Regards,
Mitch

RGV250

Hi,
I think it would be better to start a new post.
Also can you post your code so others can look at it or run it. Make it a small snippet with only the code to show the issue.
The code below sends 54321 to the serial port but if you notice String1 is 54272

Capture.JPG

Include "Amicus18.inc"

Dim MyString[5] As Byte
Dim String1 As Dword
String1 = 54321 ' Load the variable with a value
StrN MyString = Str$(Dec String1) ' Convert the Dword to a String
Do
HSerOutLn [Str MyString] ' Display the string
Loop


top204

#17
Make sure the latest version of the compilers are being used.

I tested the code listing below with an older compiler version 4.0.5.6, as well as the latest version, and it works as expected with both:

    Device = 18F25K20                                       ' Tell the compiler what device to compile for
    Declare Xtal = 16                                       ' Tell the compiler what frequency the device is operating at (in MHz)
'
' Setup USART1
'
    Declare Hserial_Baud = 9600                             ' Set the Baud rate to 9600
    Declare HRSOut1_Pin  = PORTC.6                          ' Set the TX pin
'
' Create some variables
'
    Dim MyString As String * 5                              ' Create a string variable, large enough to hold 5 characters
    Dim MyByteArray[6] As Byte                              ' Create a byte array to operate as a pseudo string with a length of 5 plus the Null
    Dim MyDWord As Dword                                    ' Create an unsigned 32-bit integer variable
          
'----------------------------------------------------------------------------
' The main program starts here
' Convert an integer variable to a pseudo string and a real string,
' and display the results on a serial terminal
'
Main:
    MyDWord = 54321                                         ' Load MyDWord with a value
    StrN MyByteArray = Str$(Dec MyDWord)                    ' Convert the Dword to a pseudo String, using a byte array
    MyString = Str$(Dec MyDWord)                            ' Convert the Dword to a real String
    Do
        HRSOutLn "MyByteArray holds: ", Str MyByteArray     ' Display the pseudo string
        HRSOutLn "MyString holds   : ", MyString            ' Display the real string
        DelayMS 200
    Loop

Notice that the Byte array operating as a pseudo String is long enough to hold the 5 characters of the value "54321", and the ending Null (value 0). i.e. A length of 6. If it is created with only 5 elements (as your code snippet had), it will overflow into the next RAM position when the Null is added to terminate the String, by the StrN command, which happened to be the MyDword variable in the code listing, and will alter its value.

The result of the above code listing, on a serial terminal is:

MyByteArray holds: 54321
MyString holds   : 54321
MyByteArray holds: 54321
MyString holds   : 54321
MyByteArray holds: 54321
MyString holds   : 54321
MyByteArray holds: 54321
MyString holds   : 54321

The StrN command is now very outdated, and is only required on the, now obsolete, standard 14-bit core devices, because the compilers have real String variables for the devices that have a linear mechanism for their indirect RAM and Flash memory operations. Which are the enhanced 14-bit core devices, 18F devices, PIC24 devices, and dsPIC33 devices.

Mitch

Hi

Thanks for this. I am integrating your test code into my environment (18F97J94). I am using the latest version of the compilker (downloaded last week).

Will post results soon.

Mitch

Hi,

Many thanks to those who commented and provided examples of code which helped me track down what was going on.

Error purely mine in not allowing a suitable size array next to the DWord variable where I was storing the serial number - filling the array overwrote the low byte of the DWord!

Thanks again

Mitch