Announcement

Collapse
No announcement yet.

Is there a simple way to convert WSTRING TO STRING in one step?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is there a simple way to convert WSTRING TO STRING in one step?

    Is there something better than:

    Code:
    LOCAL MY_WSTRING AS WSTRING
    LOCAL MY_STRING   AS STRING
    
    MY_STRING = MY_WSTRING
    to convert WSTRING TO STRING??

    I thought ACODE$(WSTRING) would have done it...

  • #2
    What trouble you having with ACODE$() ?

    With PBWin 10 and PBCC 6 even ACODE$() is not needed. Just MyStr$ = A_Wide$$
    Dale

    Comment


    • #3
      Hi Dale
      MyStr$ = A_Wide$$ is what I am doing. Was thinking there might be a CONVERT$(w_string) function or something like that. I must be thinking TSQL,,,

      Comment


      • #4
        Originally posted by David Clarke View Post
        Hi Dale
        MyStr$ = A_Wide$$ is what I am doing. Was thinking there might be a CONVERT$(w_string) function or something like that. I must be thinking TSQL,,,
        It depends on where the wide string is generated.
        In this example, it is generated by the OS and placed into a text file.
        When it is read in, it requires conversion before it is useful because it
        was read into an ascii string.
        Code:
        #COMPILE EXE
        #DIM ALL
        #DEBUG DISPLAY
        FUNCTION PBMAIN () AS LONG
            LOCAL bstr AS STRING, hfile, flen AS LONG
            hfile = FREEFILE
            OPEN "freespace.bat" FOR OUTPUT AS #hfile
            PRINT# hfile, "wmic logicaldisk GET SIZE,freespace,CAPTION > freedisk.txt"
            CLOSE #hfile
            SHELL "freespace.bat"
            OPEN "freedisk.txt" FOR BINARY AS #hfile
            flen = LOF(hfile)
            GET$ #hfile, flen, bstr
            CLOSE #hfile
            bstr =ACODE$(bstr)
            ? bstr
        END FUNCTION
            ' wmic logicaldisk GET SIZE,freespace,CAPTION
        The world is strange and wonderful.*
        I reserve the right to be horrifically wrong.
        Please maintain a safe following distance.
        *wonderful sold separately.

        Comment


        • #5
          Ah, ACODE$ was invented before WSTRING type. both ANSI/ASCII and Unicode were kept in STRING type variables (or STRINGZ * x, STRING * x). ACODE$ and UCODE$ converted the characters between 8 bit and 16 bit, but source and destination variables were both STRING type.

          Now with wide type strings the compiler includes the code automatically from convert based on declared type.

          Doing a ACODE$(WSTRING) would put ANSI characters in what we declared as a wide type. We'd be back to not knowing what kind of string is stored in a variable. (like before wide string type was added to PB.)

          Cheers,
          Dale

          Comment


          • #6
            Delphi has WideCharToString I could just make my own to keep it on one line and keep it clear.

            Comment


            • #7
              Originally posted by Kurt Kuzba View Post

              It depends on where the wide string is generated.
              In this example, it is generated by the OS and placed into a text file.
              When it is read in, it requires conversion before it is useful because it
              was read into an ascii string.
              Why not just use
              GET$$ #hfile, flen/2, bstr
              and drop the ACODE$ line
              Help GET$$
              StrgVar may be either ANSI or WIDE. If it is an ANSI variable, the data is automatically converted to ANSI bytes before it is assigned.

              Comment


              • #8
                Not sure I get you Stuart...

                EXAMPLE:
                Code:
                LOCAL COW AS WSTRING
                LOCAL DOG AS STRING
                
                COW = "MOO"            
                DOG = "WOOF"
                
                
                FUNCTION TEXT_TO_SCREEN  COW    'THIS WILL FAIL
                FUNCTION TEXT_TO_SCREEN  DOG     'THIS WILL WORK
                
                FUNCTION TEXT_TO_SCREEN  WideCharToString(COW)  'I NEED TO MAKE THIS FUCTION
                
                
                FUNCTION TEXT_TO_SCREEN (TEXT_STRING AS STRING) AS LONG
                
                  MSGBOX TEXT_STRING
                
                END FUNCTION

                Comment


                • #9
                  Code:
                   FUNCTION WideCharToString(WIDE_STRING AS WSTRING) AS STRING
                  
                     LOCAL TEMP_STRING AS STRING
                     TEMP_STRING = WIDE_STRING
                     FUNCTION = TEMP_STRING
                  
                   END FUNCTION 

                  Comment


                  • #10
                    Originally posted by David Clarke View Post
                    Not sure I get you Stuart...
                    I was referring to Kurt's example where he was using GET$ to read a Wide Character file into a String variable and then using ACODE$ to convert the read string. I was pointing out that you could use GET$$ (double"$") and the conversion is done automagically.

                    Comment


                    • #11
                      Originally posted by david clarke View Post

                      example:
                      Code:
                      #compile exe
                      #dim all
                      
                      function pbmain () as long
                      local cow as wstring
                      local dog as string
                      
                      cow = "moo"
                      dog = "woof"
                      
                      text_to_screen bycopy cow 'this works!!!
                      Text_to_screen dog 'this will work
                      end function
                      
                      function text_to_screen (text_string as string) as long
                      msgbox text_string
                      end function

                      This also works:

                      Code:
                      #COMPILE EXE
                      #DIM ALL
                      
                      FUNCTION PBMAIN () AS LONG
                      LOCAL COW AS WSTRING
                      LOCAL DOG AS STRING
                      
                      COW = "MOO"
                      DOG = "WOOF"
                      
                      TEXT_TO_SCREEN COW
                      TEXT_TO_SCREEN DOG 'THIS WILL WORK
                      END FUNCTION
                      
                      FUNCTION TEXT_TO_SCREEN (BYVAL TEXT_STRING AS STRING) AS LONG
                      MSGBOX TEXT_STRING
                      END FUNCTION

                      Comment


                      • #12
                        Originally posted by Stuart McLachlan View Post

                        Why not just use
                        GET$$ #hfile, flen/2, bstr
                        and drop the ACODE$ line
                        In that particular case, I was unaware that the string generated by the OS was wide.
                        I used the ACODE$() as a guess at why the string wouldn't display properly.
                        It worked, so I left it that way.
                        It was just a quick routine to get free drive space from the OS.
                        The world is strange and wonderful.*
                        I reserve the right to be horrifically wrong.
                        Please maintain a safe following distance.
                        *wonderful sold separately.

                        Comment


                        • #13
                          GET$$ ?????? How did it become a file operation? Question was simple way to convert wide to ANSI. Doing "=" is about as simple as it can be!
                          Dale

                          Comment


                          • #14
                            Originally posted by Dale Yarker View Post
                            GET$$ ?????? How did it become a file operation? Question was simple way to convert wide to ANSI. Doing "=" is about as simple as it can be!
                            In my example, I was reading from a file.
                            It was pointed out that GET$$ would have worked.
                            It had nothing to do with the original question.
                            The world is strange and wonderful.*
                            I reserve the right to be horrifically wrong.
                            Please maintain a safe following distance.
                            *wonderful sold separately.

                            Comment


                            • #15
                              T.A. , thought I'd missed something, thanks again.
                              Dale

                              Comment


                              • #16
                                How would you check a file for wide strings and be certain of its format?
                                reg and txt files have been of both formats.
                                p purvis

                                Comment

                                Working...
                                X