xsharp.eu • Codepage swapping
Page 1 of 1

Codepage swapping

Posted: Tue May 31, 2022 2:58 pm
by FFF
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 ;-)

Codepage swapping

Posted: Tue May 31, 2022 4:47 pm
by ic2
Hello Karl,

Somewhere in my program I am using this function, no idea where I got it from:

Code: Select all

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:

https://docs.microsoft.com/en-us/window ... towidechar

An example is this:

Code: Select all

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

Codepage swapping

Posted: Tue May 31, 2022 5:27 pm
by robert
Dick,
That code is part of the VO System Library (Util module)
I think I added it in 2004 or so.

Robert

Codepage swapping

Posted: Tue May 31, 2022 6:30 pm
by FFF
Found it, thx ;-)
As it is ptr based, ie, requests unsafe code allowed, is there an X# Version?

Codepage swapping

Posted: Tue May 31, 2022 7:04 pm
by robert
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

Codepage swapping

Posted: Tue May 31, 2022 10:14 pm
by ic2
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#:

https://docs.microsoft.com/en-us/dotnet ... em_Byte___

Dick