Jump to content


Photo

Helper script to convert from one to another codepages


  • Please log in to reply
4 replies to this topic

#1 Sean

Sean
  • Members
  • 2462 posts

Posted 09 March 2007 - 09:01 AM

This is a helper script to convert between codepages.
ANSI <-> OEM
ANSI <-> UTF8

/*
CP_ACP   = 0
CP_OEMCP = 1
CP_MACCP = 2
CP_UTF7  = 65000 
CP_UTF8  = 65001 
*/

Ansi2Oem(sString)
{
	Ansi2Unicode(sString, wString, 0)
	Unicode2Ansi(wString, zString, 1)
	Return zString
}

Oem2Ansi(zString)
{
	Ansi2Unicode(zString, wString, 1)
	Unicode2Ansi(wString, sString, 0)
	Return sString
}

Ansi2UTF8(sString)
{
	Ansi2Unicode(sString, wString, 0)
	Unicode2Ansi(wString, zString, 65001)
	Return zString
}

UTF82Ansi(zString)
{
	Ansi2Unicode(zString, wString, 65001)
	Unicode2Ansi(wString, sString, 0)
	Return sString
}

Ansi2Unicode(ByRef sString, ByRef wString, CP = 0)
{
	  nSize := DllCall("MultiByteToWideChar"
		, "Uint", CP
		, "Uint", 0
		, "Uint", &sString
		, "int",  -1
		, "Uint", 0
		, "int",  0)

	VarSetCapacity(wString, nSize * 2)

	DllCall("MultiByteToWideChar"
		, "Uint", CP
		, "Uint", 0
		, "Uint", &sString
		, "int",  -1
		, "Uint", &wString
		, "int",  nSize)
}

Unicode2Ansi(ByRef wString, ByRef sString, CP = 0)
{
	  nSize := DllCall("WideCharToMultiByte"
		, "Uint", CP
		, "Uint", 0
		, "Uint", &wString
		, "int",  -1
		, "Uint", 0
		, "int",  0
		, "Uint", 0
		, "Uint", 0)

	VarSetCapacity(sString, nSize)

	DllCall("WideCharToMultiByte"
		, "Uint", CP
		, "Uint", 0
		, "Uint", &wString
		, "int",  -1
		, "str",  sString
		, "int",  nSize
		, "Uint", 0
		, "Uint", 0)
}


#2 Joy2DWorld

Joy2DWorld
  • Members
  • 562 posts

Posted 21 April 2007 - 06:23 PM

Thanks. Was very helpful.


here is an example of how I've used it, to cleanly send Word unicode ...


text := Ansi2UTF8(text)
Transform, Clipboard, Unicode, %text%



#3 Sean

Sean
  • Members
  • 2462 posts

Posted 22 April 2007 - 07:02 AM

Thanks. Was very helpful.

Thanks.

text := Ansi2UTF8(text)
Transform, Clipboard, Unicode, %text%

I didn't know that I can use UTF-8 like this in AHK.

#4 PhiLho

PhiLho
  • Fellows
  • 6850 posts

Posted 22 April 2007 - 07:31 AM

I didn't know that I can use UTF-8 like this in AHK.

Neither me... :-)
Nor how this is better than Ansi2Unicode().

#5 Albireo

Albireo
  • Members
  • 435 posts

Posted 23 May 2012 - 10:12 PM

Hello!

I tried to use this script to convert UTF8 to ANSI.
But, I'm not getting the conversion to work. (AHK Basic)

Here I have the code.
InFile = D:\MinaDokument\EXPO\Projekt Order\Utf-8\Order1.txt

FileRead zString, %InFile%
AllRows := UTF82Ansi(zString)
; Text := UTF82Ansi(zString)
; Transform Clipboard, Unicode, %text%

MsgBox Len1 = %Len1% `nLen2 = %Len2% `n`n%AllRows%

ExitApp
	
/*
CP_ACP   = 0
CP_OEMCP = 1
CP_MACCP = 2
CP_UTF7  = 65000
CP_UTF8  = 65001
*/

Ansi2Oem(sString)
{
   Ansi2Unicode(sString, wString, 0)
   Unicode2Ansi(wString, zString, 1)
   Return zString
}

Oem2Ansi(zString)
{
   Ansi2Unicode(zString, wString, 1)
   Unicode2Ansi(wString, sString, 0)
   Return sString
}

Ansi2UTF8(sString)
{
   Ansi2Unicode(sString, wString, 0)
   Unicode2Ansi(wString, zString, 65001)
   Return zString
}

UTF82Ansi(zString)
{
   Ansi2Unicode(zString, wString, 65001)
   Unicode2Ansi(wString, sString, 0)
   Return sString
}

Ansi2Unicode(ByRef sString, ByRef wString, CP = 0)
{
     nSize := DllCall("MultiByteToWideChar"
      , "Uint", CP
      , "Uint", 0
      , "Uint", &sString
      , "int",  -1
      , "Uint", 0
      , "int",  0)

   VarSetCapacity(wString, nSize * 2)

   DllCall("MultiByteToWideChar"
      , "Uint", CP
      , "Uint", 0
      , "Uint", &sString
      , "int",  -1
      , "Uint", &wString
      , "int",  nSize)
}

Unicode2Ansi(ByRef wString, ByRef sString, CP = 0)
{
     nSize := DllCall("WideCharToMultiByte"
      , "Uint", CP
      , "Uint", 0
      , "Uint", &wString
      , "int",  -1
      , "Uint", 0
      , "int",  0
      , "Uint", 0
      , "Uint", 0)

   VarSetCapacity(sString, nSize)

   DllCall("WideCharToMultiByte"
      , "Uint", CP
      , "Uint", 0
      , "Uint", &wString
      , "int",  -1
      , "str",  sString
      , "int",  nSize
      , "Uint", 0
      , "Uint", 0)
}
All strange characters do not seem to disappear.

Before the convert, I have this characters in the begining of the file .:


After the convert, I have this characters in the begining of the file .:
¿

What's wrong?

//Jan