Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

Codepage swapping 31 May 2022 16:58 #22660

  • FFF
  • FFF's Avatar
  • Topic Author


  • Posts: 1422
  • Guys,
    i wonder, if somewhere already exists a tool, which takes a string from the clipboard and returns it re-encoded, e.g. from UTF-8 to 1252 - yes, i know there may be problems with exotic languages, but that's not relevant in my use case.
    Sample:
    In -> Tragfläche ONE, final 3 Pendel-Höhenleitwerk
    Out -> Tragfläche ONE, final 3 Pendel-Höhenleitwerk

    I think i can write something q&d myself, but would rather use what already exists ;-)
    Regards
    Karl (X# 2.16.0.5; Xide 2.16; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Last edit: by FFF.

    Codepage swapping 31 May 2022 18:47 #22662

    • ic2
    • ic2's Avatar


  • Posts: 1667
  • Hello Karl,

    Somewhere in my program I am using this function, no idea where I got it from:
    FUNCTION ConvertFromCodePageToCodePage(cString AS STRING, dwFrom AS DWORD, dwTo AS DWORD) AS STRING
       LOCAL pUStr                  AS PTR
       LOCAL pBuffer        AS PTR
       LOCAL nLen,nULen     AS LONG
       // Convert VO string from dynamic memory to fixed memory
       nLen     := LONG(SLen(cString))
       pBuffer  := MemAlloc(DWORD(nLen))
       MemCopyString(pBuffer,cString,DWORD(nLen))
    
       // Determine length of Unicode string
       // And allocate enough space to hold it
       nULen    := MultiByteToWideChar(dwFrom,0,pBuffer,nLen,NULL_PTR, 0)
       pUStr    := SysAllocStringLen(NULL_PTR,DWORD(nULen))
       // Convert Fixed memory Ansi string to Fixed memory Unicode string
       MultiByteToWideChar(dwFrom,0,pBuffer,nLen,pUStr,nULen)
    
       // Now determine size needed for ANSI string
       nLen :=      WideCharToMultiByte(dwTo,0,pUStr,nULen,NULL_PTR,0, NULL,NULL)
    
       // Allocate Fixed memory buffer to hold the UTF8 string
       pBuffer  := MemRealloc(pBuffer, DWORD(nLen+1))
       // Convert Unicode to Ansi
       nLen  := WideCharToMultiByte(dwTo,0,pUStr,nULen,pBuffer,nLen ,NULL,NULL)
    
       // Convert fixed memory buffer to dynamic memory string
       cString  := Mem2String(pBuffer,DWORD(nLen))
       // Release fixed memory buffer
       MemFree(pBuffer)
       // Release the Unicode String
       SysFreeString(pUStr)
    RETURN cString
    

    This calls MultiByteToWideChar which is documented here:

    docs.microsoft.com/en-us/windows/win32/a...-multibytetowidechar

    An example is this:
    cResult := ConvertFromCodePageToCodePage(SubStr2(cResult, 4), CP_UTF8, CP_ACP)

    and the dwFrom and dwTo constants like CP_AP or CP_UTF8 are explained in the above page.
    I hope this helps you.

    Dick

    Please Log in or Create an account to join the conversation.

    Codepage swapping 31 May 2022 19:27 #22664

    • robert
    • robert's Avatar


  • Posts: 3612
  • Dick,
    That code is part of the VO System Library (Util module)
    I think I added it in 2004 or so.

    Robert
    XSharp Development Team
    The Netherlands

    Please Log in or Create an account to join the conversation.

    Codepage swapping 31 May 2022 20:30 #22665

    • FFF
    • FFF's Avatar
    • Topic Author


  • Posts: 1422
  • Found it, thx ;-)
    As it is ptr based, ie, requests unsafe code allowed, is there an X# Version?
    Regards
    Karl (X# 2.16.0.5; Xide 2.16; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Codepage swapping 31 May 2022 21:04 #22666

    • robert
    • robert's Avatar


  • Posts: 3612
  • Karl,
    Look at the Encoding classes.
    Then do something like this:
    - Retrieve the encoding for the source string
    - Use GetBytes() to retrieve the bytes
    - Retrieve the encoding for the target string
    - Use GetString() to create the string from the bytes
    Robert
    XSharp Development Team
    The Netherlands

    Please Log in or Create an account to join the conversation.

    Codepage swapping 01 Jun 2022 00:14 #22668

    • ic2
    • ic2's Avatar


  • Posts: 1667
  • Hello Robert, Karl,

    Ah, I had it as a separate function without my usual comment lines showing where it came from. I barely escaped a lawsuit :unsure:

    Karl, you posted this in Chit-Chat so I wasn't sure if you needed VO or X#.

    This example in MSDN shows you how to do it in C#:

    docs.microsoft.com/en-us/dotnet/api/syst...oding_System_Byte___

    Dick

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1