Hi Guys
Is there any way to convert a string to a number without losing the first digit if its a 0 e.g 074 101 6666 if I convert this to an INT I get 74 101 6666 and i loose the 0 I've tried converting it to a data array but then i cant convert that to a number is there another way??
Brian
Converting strings without loosing Char
Moderator: Benj
Re: Converting strings without loosing Char
Hi Brian,
There's no way to do that directly, because the values are stored within the chip as fixed length binary numbers. e.g. if you store your number as a ULONG (4 bytes), it will always be 32bits long inside the chip. When you convert the String to an Integer, any leading zero's in the string will just be ignored, and the chip pads the number with enough 'binary zeros' to fill the bytes.
String "00001" -> Decimal Number 1 -> ULONG(Binary) 00000000 00000000 00000000 00000001
When you turn the number back into a String, it has no idea how many zeros the decimal number started out with, so just assumes that there were none. (This happens any time a computer draws a number to the screen, not just within your flowchart).
The simplest way around this would be to store somewhere the original length of the String. Then, when you come to turn your (telephone?) number back into a String, you can refer to the original length, and use that to work out how many leading zero's you need. e.g...
Even doing this, you will need to be careful - the biggest integer that FlowCode can handle as a single 'chunk' is a ULONG - and that can barely store the equivalent of 10 decimal digits. Assuming that your value is a telephone number, adding the international dialing code at the start would break the routine by making the number too large to store.
I'm guessing that you're just looking for a way to store the number that takes up less memory, or is faster to transmit, than the original string - i.e. you wouldn't be using it in a Maths calculation. There is something called 'Binary Coded Decimal' (BCD) that is often used for this. BCD is much like a String, just a sequence of bytes, but it manages to squeeze in two decimal digits per character, so halves the amount of memory needed to store them. If you think that sounds like it could be useful to your project, let me know and I'll see if I can knock together some conversion macros.
There's no way to do that directly, because the values are stored within the chip as fixed length binary numbers. e.g. if you store your number as a ULONG (4 bytes), it will always be 32bits long inside the chip. When you convert the String to an Integer, any leading zero's in the string will just be ignored, and the chip pads the number with enough 'binary zeros' to fill the bytes.
String "00001" -> Decimal Number 1 -> ULONG(Binary) 00000000 00000000 00000000 00000001
When you turn the number back into a String, it has no idea how many zeros the decimal number started out with, so just assumes that there were none. (This happens any time a computer draws a number to the screen, not just within your flowchart).
The simplest way around this would be to store somewhere the original length of the String. Then, when you come to turn your (telephone?) number back into a String, you can refer to the original length, and use that to work out how many leading zero's you need. e.g...
Code: Select all
/* Convert to Integer */
phone_no_string = "01422252380"
original_length = Length$(phone_no_string)
phone_no_integer = StringToInt$(phone_no_string)
/* Convert back to String */
phone_no_string = ToString$(phone_no_integer)
zero_padding = original_length - Length$(phone_no_string)
phone_no_string = Left$("000000000000", zero_padding) + phone_no_string
I'm guessing that you're just looking for a way to store the number that takes up less memory, or is faster to transmit, than the original string - i.e. you wouldn't be using it in a Maths calculation. There is something called 'Binary Coded Decimal' (BCD) that is often used for this. BCD is much like a String, just a sequence of bytes, but it manages to squeeze in two decimal digits per character, so halves the amount of memory needed to store them. If you think that sounds like it could be useful to your project, let me know and I'll see if I can knock together some conversion macros.
- jollybv
- Flowcode v5 User
- Posts: 376
- Joined: Thu Feb 12, 2009 5:20 am
- Location: Cape Town
- Has thanked: 81 times
- Been thanked: 25 times
Re: Converting strings without loosing Char
Hi Steve
Thanks yes it is a phone number I'm trying to store i would like to store 500 to 1000 numbers but a 16k bit eeprom wont come close to that so I was thinking of using a 1 or 2Gb SD Card with the FAT1 Component do you think this will work or is it to complicated. Im not quite sure if the PIC16LF1939 will be able to talke to a SD CARD?? Ive serched and I cant find any V6 examples of how to write to the SD card. I so need to move forward on this project been stumped for a month now. Please do a conversation macro so I can see witch way to go.
Brian
Thanks yes it is a phone number I'm trying to store i would like to store 500 to 1000 numbers but a 16k bit eeprom wont come close to that so I was thinking of using a 1 or 2Gb SD Card with the FAT1 Component do you think this will work or is it to complicated. Im not quite sure if the PIC16LF1939 will be able to talke to a SD CARD?? Ive serched and I cant find any V6 examples of how to write to the SD card. I so need to move forward on this project been stumped for a month now. Please do a conversation macro so I can see witch way to go.
Brian
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Converting strings without loosing Char
Hello Brian,
The FAT component might only work with 18F devices in the PIC range due to stack and memory constraints. I would try adding the component to your program and calling the init macro to see if it will compile the code.
Otherwise yes the FAT component is a very nice way of reading/writing vast amounts of data at low cost compared to an EEPROM device.
The FAT component might only work with 18F devices in the PIC range due to stack and memory constraints. I would try adding the component to your program and calling the init macro to see if it will compile the code.
Otherwise yes the FAT component is a very nice way of reading/writing vast amounts of data at low cost compared to an EEPROM device.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- jollybv
- Flowcode v5 User
- Posts: 376
- Joined: Thu Feb 12, 2009 5:20 am
- Location: Cape Town
- Has thanked: 81 times
- Been thanked: 25 times
Re: Converting strings without loosing Char
Hi Benj
Thanks I did incert the FAT1 inisilise into the cod and compile it to the PIC16F1939 and it worked so im thinking that is the way to go i will make a small board with the card reader on it and then move on from there is there any exsample cod that i can see how the FAT component works??
Brian
Thanks I did incert the FAT1 inisilise into the cod and compile it to the PIC16F1939 and it worked so im thinking that is the way to go i will make a small board with the card reader on it and then move on from there is there any exsample cod that i can see how the FAT component works??
Brian
-
- Valued Contributor
- Posts: 2045
- Joined: Wed Aug 27, 2008 10:31 pm
- Location: Netherlands
- Has thanked: 553 times
- Been thanked: 1081 times
Re: Converting strings without loosing Char
Perhaps you could try to find it yourself before asking? It took google 2 seconds to find the help page on the WIKI with example code on it. (searched for "Flowcode FAT")
“Integrity is doing the right thing, even when no one is watching.”
― C.S. Lewis
― C.S. Lewis
- jollybv
- Flowcode v5 User
- Posts: 376
- Joined: Thu Feb 12, 2009 5:20 am
- Location: Cape Town
- Has thanked: 81 times
- Been thanked: 25 times
Re: Converting strings without loosing Char
Hi Kirsing
I did search for it in the forum and couldnt find very much but did not do a Google search but thanks for this.
I did search for it in the forum and couldnt find very much but did not do a Google search but thanks for this.
- jollybv
- Flowcode v5 User
- Posts: 376
- Joined: Thu Feb 12, 2009 5:20 am
- Location: Cape Town
- Has thanked: 81 times
- Been thanked: 25 times
Re: Converting strings without loosing Char
Hi Benj
0ne quick question I want to test this on my hardware and I'm running a PIC16F1939 at 19.6608M how would i change the frequance of the sdcard (scandisk 4Gb) to 400Kh would i need to change the Fosc/4 to Fosc/16 or 64 how is this worked out.
Brian
0ne quick question I want to test this on my hardware and I'm running a PIC16F1939 at 19.6608M how would i change the frequance of the sdcard (scandisk 4Gb) to 400Kh would i need to change the Fosc/4 to Fosc/16 or 64 how is this worked out.
Brian
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Converting strings without loosing Char
Hi Brian,
On a 8-bit PIC you use the following calc.
Fosc = 19.6608M / 4
Fosc / 4 = 1.2288MHz
Fosc / 16 = 307.2KHz
Fosc / 64 = 76.8KHz
On a 8-bit PIC you use the following calc.
Fosc = 19.6608M / 4
Fosc / 4 = 1.2288MHz
Fosc / 16 = 307.2KHz
Fosc / 64 = 76.8KHz
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel