Codepage swapping

This forum is meant for anything you would like to share with other visitors
Post Reply
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Codepage swapping

Post 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 ;-)
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Codepage swapping

Post 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
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Codepage swapping

Post by robert »

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
robert@xsharp.eu
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Codepage swapping

Post by FFF »

Found it, thx ;-)
As it is ptr based, ie, requests unsafe code allowed, is there an X# Version?
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
robert
Posts: 4520
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Codepage swapping

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Codepage swapping

Post 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
Post Reply