dromenox Posted January 23, 2015 Share Posted January 23, 2015 I made a simple xor encryption to use in a program in C++. I want the process to be very fast so I want the AutoIt returns a string ready for me to paste in C++ Func xor_crypt($string) Local $stringarr = StringSplit($string, "") Local $n Local $ret = "" For $n = 1 To $stringarr[0] $ret &= "\x" & StringRight(Hex(BitXOR(Asc($stringarr[$n]), 78)), 2) Next Return $ret EndFunc In C++: char* Decrypt(char string[]) { int length = strlen(string); char* tempstring = new char[length + 1]; for (int i = 0; i < length; i ++) tempstring[i] = string[i] ^ 78; tempstring[length] = 0; return tempstring; } Everything is working, I have tested. But I want the result to be like this: Input: xor_crypt("Test") Output(a String): Decrypt("\x0D\x26\x2F\x23\x3D"/*Test*/); Sorry for the english. Link to comment Share on other sites More sharing options...
UEZ Posted January 23, 2015 Share Posted January 23, 2015 (edited) Something like that here?ConsoleWrite(xor_crypt("Test") & @CRLF) ConsoleWrite(xor_crypt2("Test") & @CRLF) Func xor_crypt($string) Local $stringarr = StringSplit($string, ""), $n, $ret = "" For $n = 1 To $stringarr[0] $ret &= "\x" & Hex(BitXOR(Asc($stringarr[$n]), 78), 2) Next Return '"' & $ret & '"/*' & $string & '*/' EndFunc Func xor_crypt2($string) Local $n, $ret = "" For $n = 1 To StringLen($string) $ret &= "\x" & Hex(BitXOR(Asc(StringMid($string, $n, 1)), 78), 2) Next Return '"' & $ret & '"/*' & $string & '*/' EndFuncBr,UEZ Edited January 23, 2015 by UEZ Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
guinness Posted January 23, 2015 Share Posted January 23, 2015 StringToASCIIArray could also be utlised here. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
guinness Posted January 23, 2015 Share Posted January 23, 2015 Slightly shorted >> ConsoleWrite(XOR_Crypt("Test") & @CRLF) Func XOR_Crypt($sString) Local $aChars = StringToASCIIArray($sString) If @error Then Return SetError(@error, @extended, "") Local $sReturn = "" For $i = 0 To UBound($aChars) - 1 $sReturn &= "\x" & Hex(BitXOR($aChars[$i], 78), 2) Next Return '"' & $sReturn & '"' & "/*" & $sString & "*/" EndFunc ;==>XOR_Crypt UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
DXRW4E Posted January 23, 2015 Share Posted January 23, 2015 (edited) Is not correct since the beginning, to say more good since C++ function, so Autoit is OK, is Full comatibile with Windows Mod, so will have to be also the C++ func, Windows Mod is CryptStringToBinary & CryptBinaryToString, example expandcollapse popup/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // BinaryToStringEx // The BinaryToStringEx function converts a Binary (UTF16 Little Endian) variant into a formatted String. // // PARAMETERS: // pszBinary _In_ A pointer to the array of bytes (String Hex Mod) to be converted into a string // cchString _In_ The number of characters of the pszBinary (String Hex Mod) to be converted, not // including the terminating NULL character. If this parameter is zero, pszBinary // is considered to be a null-terminated string. // Remember that 4 characters of the Binary (String Hex Mod) = 1 characters of formatted String // // RETURN: // Success: Pointer of the formatted (Wide Characters) String // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use delete[] pszBinary; to release the memory // Remember that 4 characters of the Binary (String Hex Mod) = 1 characters of formatted String // so if pszBinary is less than 4 characters, BinaryToStringEx return nullptr and set ERROR_INVALID_DATA // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPCWSTR BinaryToStringEx(LPCWSTR pszBinary, size_t cchString) { wchar_t pHex[5], *pszString; if (!(cchString /= 4) && !(cchString = (wcslen(pszBinary) / 4))) { SetLastError(ERROR_INVALID_DATA); return nullptr; } else if (!(pszString = new (std::nothrow) wchar_t[cchString + 1]/*()*/)) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } pszString[cchString] = wcNULL; for (LPWSTR pString = pszString; cchString != 0; --cchString, pszBinary += 4, ++pString) { wcsncpy_s(pHex, 5, pszBinary + 2, 2); wcsncat_s(pHex, 5, pszBinary, 2); *pString = (wchar_t)wcstoul(pHex, NULL, 16); } return pszString; //const_cast<LPCWSTR>(pszString); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // StringToBinaryEx // The StringToBinaryEx function converts a formatted string into an Binary (UTF16 Little Endian) String Hex Mod. // // PARAMETERS: // pszString _In_ A pointer to a string that contains the formatted string to be converted. // cchString _In_ The number of characters of the formatted string to be converted, not // including the terminating NULL character. If this parameter is zero, pszString // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of array of bytes (Binary UTF16 Little Endian, String Hex Mod) // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use delete[] Pointer; to release the memory // Remember that 1 characters of formatted String = 4 characters of the Binary (String Hex Mod) // so the limit of (cchString) characters to converted in _WIN32 is 0x1FFFFFFC (_HEAP_MAXREQ / sizeof(wchar_t) / 4) // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPCWSTR StringToBinaryEx(LPCWSTR pszString, size_t cchString) { if (!cchString && !(cchString = wcslen(pszString))) { SetLastError(ERROR_INVALID_DATA); return nullptr; } LPWSTR pszBinary, pwsBinary; size_t uCount = ((cchString > _MAX_RBS_ ? _RAW_BIN_STR_ : cchString) * 4) + (cchString < _RAW_BIN_STR_); if (!(pszBinary = new (std::nothrow) wchar_t[uCount])) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } pwsBinary = pszBinary; pszBinary[uCount - 1] = wcNULL; for (LPBYTE pbBinary = (LPBYTE)pszString; cchString != 0; --cchString, pwsBinary += 4, pbBinary += 2) { swprintf(pwsBinary, uCount, L"%02X%02X", *pbBinary, *(pbBinary + 1)); } return pszBinary; //const_cast<LPCWSTR>(pszBinary); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // BinaryToString // The BinaryToString function converts a Binary (UTF16 Little Endian) variant into a formatted String. // // PARAMETERS: // pszBinary _In_ A pointer to the array of bytes (String Hex Mod) to be converted into a string // cchString _In_ The number of characters of the pszBinary (String Hex Mod) to be converted, not // including the terminating NULL character. If this parameter is zero, pszBinary // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of the formatted (Wide Characters) String // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use delete[] pszBinary; to release the memory // Remember that 4 characters of the Binary (String Hex Mod) = 1 characters of formatted String // so if pszBinary is less than 4 characters, BinaryToStringEx return nullptr and set ERROR_INVALID_DATA // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPCWSTR BinaryToString(LPCWSTR pszBinary, DWORD cchString) { LPWSTR pszString; DWORD cbBinary; //, pdwSkip, pdwFlags; if (!CryptStringToBinaryW(pszBinary, cchString, CRYPT_STRING_HEXRAW, NULL, &cbBinary, NULL, NULL)) { return nullptr; } else if (!(pszString = new (std::nothrow) wchar_t[(cbBinary / 2) + 1]/*()*/)) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } //if (!CryptStringToBinaryW(pszBinary, cchString, CRYPT_STRING_HEXRAW, (LPBYTE)pszString, &cbBinary, &pdwSkip, &pdwFlags)) { return nullptr; }; if (!CryptStringToBinaryW(pszBinary, cchString, CRYPT_STRING_HEXRAW, (LPBYTE)pszString, &cbBinary, NULL, NULL)) { delete[] pszString; return nullptr; } pszString[cbBinary / 2] = wcNULL; return pszString; //const_cast<LPCWSTR>(pszString); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // StringToBinary // The StringToBinary function converts a formatted string into an Binary (UTF16 Little Endian) String Hex Mod. // // PARAMETERS: // pszString _In_ A pointer to a string that contains the formatted string to be converted. // cchString _In_ The number of characters of the formatted string to be converted, not // including the terminating NULL character. If this parameter is zero, pszString // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of array of bytes (Binary UTF16 Little Endian, String Hex Mod) // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use delete[] Pointer; to release the memory // Remember that 1 characters of formatted String = 4 characters of the Binary (String Hex Mod) // so the limit of (cchString) characters to converted in _WIN32 is 0x1FFFFFFC (_HEAP_MAXREQ / sizeof(wchar_t) / 4) // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPCWSTR StringToBinary(LPCWSTR pszString, size_t cchString) { if (!cchString && !(cchString = wcslen(pszString))) { SetLastError(ERROR_INVALID_DATA); return nullptr; } DWORD cbBinary = (DWORD)((cchString > _MAX_RBS_X32 ? _RAW_BIN_STR_X32 : cchString) * sizeof(wchar_t)) + (cchString < _RAW_BIN_STR_X32), dwCount = 0; LPWSTR pszBinary; LPCBYTE pbBinary = (LPBYTE)pszString; if (!CryptBinaryToStringW(pbBinary, cbBinary, CRYPT_STRING_HEXRAW_NOCRLF, NULL, &dwCount)) { return nullptr; } else if (!(pszBinary = new (std::nothrow) wchar_t[dwCount])) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } pszBinary[dwCount - 1] = wcNULL; if (!CryptBinaryToStringW(pbBinary, cbBinary, CRYPT_STRING_HEXRAW_NOCRLF, pszBinary, &dwCount)) { delete[] pszBinary; return nullptr; } for (LPWSTR pBinary = pszBinary; dwCount != 0; --dwCount, ++pBinary) { *pBinary = towupper(*pBinary); } return pszBinary; //const_cast<LPCWSTR>(pszBinary); } after use in AutoIt StringToBinary & BinaryToString my personal StringBinary.h, the beauty of C ++ is that sometimes you can create function, more good and faster than those of the same windows api, as in this case the BinaryToStringEx & StringToBinaryEx are the fastest, and more safe because the CRYPT_STRING_HEXRAW && CRYPT_STRING_NOCRLF are not supported in Windows Server 2003 and Windows XP expandcollapse popup#include <windows.h> #include <string> #include <Wincrypt.h> #pragma comment(lib, "Crypt32.lib") //#ifndef UNICODE //#define UNICODE //#endif //#ifndef _UNICODE //#define _UNICODE //#endif //#include <ODSEx.h> //#include <QueryPerformance.h> #ifndef _HEAP_MAXREQ_X32 #define _HEAP_MAXREQ_X32 0xFFFFFFE0 #endif /* _HEAP_MAXREQ_X32 */ #ifndef wcNULL #define wcNULL L'\0' // Null character #endif #ifndef wsNULL #define wsNULL L"\0" // Null character #endif #ifndef wsNull #define wsNull std::wstring(1, 0) //*(new std::wstring(/*1, 0*/)) ////#define wsNull *(new std::wstring(1, 0)) #endif #ifndef _RAW_BIN_STR_ ////#define _RAW_BIN_STR_ (_HEAP_MAXREQ / sizeof(wchar_t) / 4) #ifdef _WIN64 #define _RAW_BIN_STR_ 0x1FFFFFFFFFFFFFFC #else /* _WIN64 */ #define _RAW_BIN_STR_ 0x1FFFFFFC #endif /* _WIN64 */ #endif /* _RAW_BIN_STR_ */ #ifndef _RAW_BIN_STR_X32 #define _RAW_BIN_STR_X32 0x1FFFFFFC #endif /* _RAW_BIN_STR_X32 */ #ifndef _MAX_RBS_ ////#define _MAX_RBS_ (_RAW_BIN_STR_ - 1) #ifdef _WIN64 #define _MAX_RBS_ 0x1FFFFFFFFFFFFFFB #else /* _WIN64 */ #define _MAX_RBS_ 0x1FFFFFFB #endif /* _WIN64 */ #endif /* _MAX_RBS_ */ #ifndef _MAX_RBS_X32 #define _MAX_RBS_X32 0x1FFFFFFB #endif /* _MAX_RBS_X32 */ #define CRYPT_STRING_HEXRAW_NOCRLF 0x4000000c //// CRYPT_STRING_HEXRAW + CRYPT_STRING_NOCRLF LPWSTR BinaryToStringEx(LPWSTR pszBinary, size_t cchString = NULL); LPCWSTR BinaryToStringEx(LPCWSTR pszBinary, size_t cchString = NULL); LPWSTR _BinaryToStringEx(LPWSTR pszBinary, size_t cchString = NULL); LPCWSTR _BinaryToStringEx(LPCWSTR pszBinary, size_t cchString = NULL); std::wstring BinaryToStringEx(std::wstring& wsBinary, size_t cchString = NULL); std::wstring* _BinaryToStringEx(std::wstring& wsHex, size_t cchString = NULL); LPWSTR BinaryToString(LPWSTR pszBinary, DWORD cchString = NULL); LPCWSTR BinaryToString(LPCWSTR pszBinary, DWORD cchString = NULL); LPWSTR _BinaryToString(LPWSTR pszBinary, DWORD cchString = NULL); LPCWSTR _BinaryToString(LPCWSTR pszBinary, DWORD cchString = NULL); std::wstring BinaryToString(std::wstring& wsBinary, size_t cchString = NULL); std::wstring* _BinaryToString(std::wstring& wsBinary, size_t cchString = NULL); LPWSTR StringToBinaryEx(LPWSTR pszString, size_t cchString = NULL); LPCWSTR StringToBinaryEx(LPCWSTR pszString, size_t cchString = NULL); LPWSTR _StringToBinaryEx(LPWSTR pszString, size_t cchString = NULL); LPCWSTR _StringToBinaryEx(LPCWSTR pszString, size_t cchString = NULL); std::wstring StringToBinaryEx(std::wstring& wsString, size_t cchString = NULL); std::wstring* _StringToBinaryEx(std::wstring& wsString, size_t cchString = NULL); LPWSTR StringToBinary(LPWSTR pszString, size_t cchString = NULL); LPCWSTR StringToBinary(LPCWSTR pszString, size_t cchString = NULL); LPWSTR _StringToBinary(LPWSTR pszString, size_t cchString = NULL); LPCWSTR _StringToBinary(LPCWSTR pszString, size_t cchString = NULL); std::wstring StringToBinary(std::wstring& wsString, size_t cchString = NULL); std::wstring* _StringToBinary(std::wstring& wsString, size_t cchString = NULL); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // BinaryToStringEx // The BinaryToStringEx function converts a Binary (UTF16 Little Endian) variant into a formatted String. // // PARAMETERS: // pszBinary _In_ A pointer to the array of bytes (String Hex Mod) to be converted into a string // cchString _In_ The number of characters of the pszBinary (String Hex Mod) to be converted, not // including the terminating NULL character. If this parameter is zero, pszBinary // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of the formatted (Wide Characters) String // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use delete[] pszBinary; to release the memory // Remember that 4 characters of the Binary (String Hex Mod) = 1 characters of formatted String // so if pszBinary is less than 4 characters, BinaryToStringEx return nullptr and set ERROR_INVALID_DATA // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPWSTR BinaryToStringEx(LPWSTR pszBinary, size_t cchString) { wchar_t pHex[5], *pszString; if (!(cchString /= 4) && !(cchString = (wcslen(pszBinary) / 4))) { SetLastError(ERROR_INVALID_DATA); return nullptr; } else if (!(pszString = new (std::nothrow) wchar_t[cchString + 1]/*()*/)) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } pszString[cchString] = wcNULL; for (LPWSTR pString = pszString; cchString != 0; --cchString, pszBinary += 4, ++pString) { wcsncpy_s(pHex, 5, pszBinary + 2, 2); wcsncat_s(pHex, 5, pszBinary, 2); *pString = (wchar_t)wcstoul(pHex, NULL, 16); } return pszString; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // BinaryToStringEx // The BinaryToStringEx function converts a Binary (UTF16 Little Endian) variant into a formatted String. // // PARAMETERS: // pszBinary _In_ A pointer to the array of bytes (String Hex Mod) to be converted into a string // cchString _In_ The number of characters of the pszBinary (String Hex Mod) to be converted, not // including the terminating NULL character. If this parameter is zero, pszBinary // is considered to be a null-terminated string. // Remember that 4 characters of the Binary (String Hex Mod) = 1 characters of formatted String // // RETURN: // Success: Pointer of the formatted (Wide Characters) String // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use delete[] pszBinary; to release the memory // Remember that 4 characters of the Binary (String Hex Mod) = 1 characters of formatted String // so if pszBinary is less than 4 characters, BinaryToStringEx return nullptr and set ERROR_INVALID_DATA // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPCWSTR BinaryToStringEx(LPCWSTR pszBinary, size_t cchString) { wchar_t pHex[5], *pszString; if (!(cchString /= 4) && !(cchString = (wcslen(pszBinary) / 4))) { SetLastError(ERROR_INVALID_DATA); return nullptr; } else if (!(pszString = new (std::nothrow) wchar_t[cchString + 1]/*()*/)) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } pszString[cchString] = wcNULL; for (LPWSTR pString = pszString; cchString != 0; --cchString, pszBinary += 4, ++pString) { wcsncpy_s(pHex, 5, pszBinary + 2, 2); wcsncat_s(pHex, 5, pszBinary, 2); *pString = (wchar_t)wcstoul(pHex, NULL, 16); } return pszString; //const_cast<LPCWSTR>(pszString); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // _BinaryToStringEx // The __BinaryToStringEx function converts a Binary (UTF16 Little Endian) variant into a formatted String. // // PARAMETERS: // pszBinary _In_ A pointer to the array of bytes (String Hex Mod) to be converted into a string // cchString _In_ The number of characters of the pszBinary (String Hex Mod) to be converted, not // including the terminating NULL character. If this parameter is zero, pszBinary // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of the formatted (Wide Characters) String // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use free(pszBinary); to release the memory // Remember that 4 characters of the Binary (String Hex Mod) = 1 characters of formatted String // so if pszBinary is less than 4 characters, _BinaryToStringEx return nullptr and set ERROR_INVALID_DATA // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPWSTR _BinaryToStringEx(LPWSTR pszBinary, size_t cchString) { wchar_t pHex[5] = { wcNULL, wcNULL, wcNULL, wcNULL, wcNULL }; LPWSTR pszString; if (!(cchString /= 4) && !(cchString = (wcslen(pszBinary) / 4))) { SetLastError(ERROR_INVALID_DATA); return nullptr; } else if (!(pszString = (LPWSTR)malloc((cchString + 1) * sizeof(wchar_t)))) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } pszString[cchString] = wcNULL; for (LPWSTR pString = pszString; cchString != 0; --cchString, ++pszBinary, ++pString) { pHex[2] = *pszBinary; pHex[3] = *++pszBinary; pHex[0] = *++pszBinary; pHex[1] = *++pszBinary; *pString = (wchar_t)wcstoul(pHex, NULL, 16); } return pszString; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // _BinaryToStringEx // The BinaryToStringEx function converts a Binary (UTF16 Little Endian) variant into a formatted String. // // PARAMETERS: // pszBinary _In_ A pointer to the array of bytes (String Hex Mod) to be converted into a string // cchString _In_ The number of characters of the pszBinary (String Hex Mod) to be converted, not // including the terminating NULL character. If this parameter is zero, pszBinary // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of the formatted (Wide Characters) String // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use free(pszBinary); to release the memory // Remember that 4 characters of the Binary (String Hex Mod) = 1 characters of formatted String // so if pszBinary is less than 4 characters, _BinaryToStringEx return nullptr and set ERROR_INVALID_DATA // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPCWSTR _BinaryToStringEx(LPCWSTR pszBinary, size_t cchString) { wchar_t pHex[5] = { wcNULL, wcNULL, wcNULL, wcNULL, wcNULL }; LPWSTR pszString; if (!(cchString /= 4) && !(cchString = (wcslen(pszBinary) / 4))) { SetLastError(ERROR_INVALID_DATA); return nullptr; } else if (!(pszString = (LPWSTR)malloc((cchString + 1) * sizeof(wchar_t)))) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } pszString[cchString] = wcNULL; for (LPWSTR pString = pszString; cchString != 0; --cchString, ++pszBinary, ++pString) { pHex[2] = *pszBinary; pHex[3] = *++pszBinary; pHex[0] = *++pszBinary; pHex[1] = *++pszBinary; *pString = (wchar_t)wcstoul(pHex, NULL, 16); } return pszString; //const_cast<LPCWSTR>(pszString); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // BinaryToStringEx // The BinaryToStringEx function converts a Binary (UTF16 Little Endian) variant into a formatted String. // // PARAMETERS: // wsBinary _In_ A std::wstring of bytes (String Hex Mod) to be converted into a string // cchString _In_ The Length of wsBinary, the number of characters of the Binary (String Hex Mod) // to be converted, not including the terminating NULL character. // // RETURN: // Success: std::wstring that contains the formatted string // Failure: NULL (empty) std::wstring, For extended error information, call GetLastError. // // NOTE: // Remember that 4 characters of the Binary (String Hex Mod) = 1 characters of formatted String // so if wsBinary is less than 4 characters, BinaryToStringEx return NULL (empty) std::wstring and set ERROR_INVALID_DATA // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// std::wstring BinaryToStringEx(std::wstring& wsBinary, size_t cchString) { if (!(cchString /= 4) && !(cchString = (wsBinary.length() / 4))) { SetLastError(ERROR_INVALID_DATA); return std::wstring(); } std::wstring* wsString; try { wsString = new /*(std::nothrow)*/ std::wstring(cchString, wcNULL); } catch (const std::exception&) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return std::wstring(); } wchar_t pHex[5], *pString = const_cast<LPWSTR>(wsString->c_str()); for (LPCWSTR pBinaryString = wsBinary.c_str(); cchString != 0; --cchString, pBinaryString += 4, ++pString) { wcsncpy_s(pHex, 5, pBinaryString + 2, 2); wcsncat_s(pHex, 5, pBinaryString, 2); *pString = (wchar_t)wcstoul(pHex, NULL, 16); } return *wsString; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // _BinaryToStringEx // The BinaryToStringEx function converts a Binary (UTF16 Little Endian) variant into a formatted String. // // PARAMETERS: // wsBinary _In_ A std::wstring of bytes (String Hex Mod) to be converted into a string // cchString _In_ The Length of wsBinary, the number of characters of the Binary (String Hex Mod) // to be converted, not including the terminating NULL character. // // RETURN: // Success: Pointer of std::wstring that contains the formatted string // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Remember that 4 characters of the Binary (String Hex Mod) = 1 characters of formatted String // so if wsBinary is less than 4 characters, _BinaryToStringEx return nullptr and set ERROR_INVALID_DATA // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// std::wstring* _BinaryToStringEx(std::wstring& wsBinary, size_t cchString) { wchar_t pHex[5] = { wcNULL, wcNULL, wcNULL, wcNULL, wcNULL }; if (!(cchString /= 4) && !(cchString = (wsBinary.length() / 4))) { SetLastError(ERROR_INVALID_DATA); return nullptr; } std::wstring* wsString; try { wsString = new /*(std::nothrow)*/ std::wstring(cchString++, wcNULL); } catch (const std::exception&) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } for (LPWSTR pString = const_cast<LPWSTR>(wsString->c_str()), pszBinary = const_cast<LPWSTR>(wsBinary.c_str()); --cchString; ++pszBinary, ++pString) { pHex[2] = *pszBinary; pHex[3] = *++pszBinary; pHex[0] = *++pszBinary; pHex[1] = *++pszBinary; *pString = (wchar_t)wcstoul(pHex, NULL, 16); } return wsString; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // BinaryToString // The BinaryToString function converts a Binary (UTF16 Little Endian) variant into a formatted String. // // PARAMETERS: // pszBinary _In_ A pointer to the array of bytes (String Hex Mod) to be converted into a string // cchString _In_ The number of characters of the pszBinary (String Hex Mod) to be converted, not // including the terminating NULL character. If this parameter is zero, pszBinary // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of the formatted (Wide Characters) String // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use delete[] pszBinary; to release the memory // Remember that 4 characters of the Binary (String Hex Mod) = 1 characters of formatted String // so if pszBinary is less than 4 characters, BinaryToStringEx return nullptr and set ERROR_INVALID_DATA // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPWSTR BinaryToString(LPWSTR pszBinary, DWORD cchString) { LPWSTR pszString; DWORD cbBinary; //, pdwSkip, pdwFlags; if (!CryptStringToBinaryW(pszBinary, cchString, CRYPT_STRING_HEXRAW, NULL, &cbBinary, NULL, NULL)) { return nullptr; } else if (!(pszString = new (std::nothrow) wchar_t[(cbBinary / 2) + 1]/*()*/)) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } //if (!CryptStringToBinaryW(pszBinary, cchString, CRYPT_STRING_HEXRAW, (LPBYTE)pszString, &cbBinary, &pdwSkip, &pdwFlags)) { return nullptr; }; if (!CryptStringToBinaryW(pszBinary, cchString, CRYPT_STRING_HEXRAW, (LPBYTE)pszString, &cbBinary, NULL, NULL)) { delete[] pszString; return nullptr; } pszString[cbBinary / 2] = wcNULL; return pszString; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // BinaryToString // The BinaryToString function converts a Binary (UTF16 Little Endian) variant into a formatted String. // // PARAMETERS: // pszBinary _In_ A pointer to the array of bytes (String Hex Mod) to be converted into a string // cchString _In_ The number of characters of the pszBinary (String Hex Mod) to be converted, not // including the terminating NULL character. If this parameter is zero, pszBinary // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of the formatted (Wide Characters) String // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use delete[] pszBinary; to release the memory // Remember that 4 characters of the Binary (String Hex Mod) = 1 characters of formatted String // so if pszBinary is less than 4 characters, BinaryToStringEx return nullptr and set ERROR_INVALID_DATA // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPCWSTR BinaryToString(LPCWSTR pszBinary, DWORD cchString) { LPWSTR pszString; DWORD cbBinary; //, pdwSkip, pdwFlags; if (!CryptStringToBinaryW(pszBinary, cchString, CRYPT_STRING_HEXRAW, NULL, &cbBinary, NULL, NULL)) { return nullptr; } else if (!(pszString = new (std::nothrow) wchar_t[(cbBinary / 2) + 1]/*()*/)) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } //if (!CryptStringToBinaryW(pszBinary, cchString, CRYPT_STRING_HEXRAW, (LPBYTE)pszString, &cbBinary, &pdwSkip, &pdwFlags)) { return nullptr; }; if (!CryptStringToBinaryW(pszBinary, cchString, CRYPT_STRING_HEXRAW, (LPBYTE)pszString, &cbBinary, NULL, NULL)) { delete[] pszString; return nullptr; } pszString[cbBinary / 2] = wcNULL; return pszString; //const_cast<LPCWSTR>(pszString); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // _BinaryToString // The BinaryToString function converts a Binary (UTF16 Little Endian) variant into a formatted String. // // PARAMETERS: // pszBinary _In_ A pointer to the array of bytes (String Hex Mod) to be converted into a string // cchString _In_ The number of characters of the pszBinary (String Hex Mod) to be converted, not // including the terminating NULL character. If this parameter is zero, pszBinary // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of the formatted (Wide Characters) String // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use free(pszBinary); to release the memory // Remember that 4 characters of the Binary (String Hex Mod) = 1 characters of formatted String // so if pszBinary is less than 4 characters, _BinaryToStringEx return nullptr and set ERROR_INVALID_DATA // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPWSTR _BinaryToString(LPWSTR pszBinary, DWORD cchString) { LPWSTR pszString; DWORD cbBinary; //, pdwSkip, pdwFlags; if (!CryptStringToBinaryW(pszBinary, cchString, CRYPT_STRING_HEXRAW, NULL, &cbBinary, NULL, NULL)) { return nullptr; } else if (!(pszString = (LPWSTR)malloc(((cbBinary / 2) + 1) * sizeof(wchar_t)))) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } //if (!CryptStringToBinaryW(pszBinary, cchString, CRYPT_STRING_HEXRAW, (LPBYTE)pszString, &cbBinary, &pdwSkip, &pdwFlags)) { return nullptr; }; if (!CryptStringToBinaryW(pszBinary, cchString, CRYPT_STRING_HEXRAW, (LPBYTE)pszString, &cbBinary, NULL, NULL)) { free(pszString); return nullptr; } pszString[cbBinary / 2] = wcNULL; return pszString; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // _BinaryToString // The BinaryToString function converts a Binary (UTF16 Little Endian) variant into a formatted String. // // PARAMETERS: // pszBinary _In_ A pointer to the array of bytes (String Hex Mod) to be converted into a string // cchString _In_ The number of characters of the pszBinary (String Hex Mod) to be converted, not // including the terminating NULL character. If this parameter is zero, pszBinary // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of the formatted (Wide Characters) String // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use free(pszBinary); to release the memory // Remember that 4 characters of the Binary (String Hex Mod) = 1 characters of formatted String // so if pszBinary is less than 4 characters, _BinaryToStringEx return nullptr and set ERROR_INVALID_DATA // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPCWSTR _BinaryToString(LPCWSTR pszBinary, DWORD cchString) { LPWSTR pszString; DWORD cbBinary; //, pdwSkip, pdwFlags; if (!CryptStringToBinaryW(pszBinary, cchString, CRYPT_STRING_HEXRAW, NULL, &cbBinary, NULL, NULL)) { return nullptr; } else if (!(pszString = (LPWSTR)malloc(((cbBinary / 2) + 1) * sizeof(wchar_t)))) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } //if (!CryptStringToBinaryW(pszBinary, cchString, CRYPT_STRING_HEXRAW, (LPBYTE)pszString, &cbBinary, &pdwSkip, &pdwFlags)) { return nullptr; }; if (!CryptStringToBinaryW(pszBinary, cchString, CRYPT_STRING_HEXRAW, (LPBYTE)pszString, &cbBinary, NULL, NULL)) { free(pszString); return nullptr; } pszString[cbBinary / 2] = wcNULL; return pszString; //const_cast<LPCWSTR>(pszString); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // BinaryToString // The BinaryToString function converts a Binary (UTF16 Little Endian) variant into a formatted String. // // PARAMETERS: // wsBinary _In_ A std::wstring of bytes (String Hex Mod) to be converted into a string // cchString _In_ The Length of wsBinary, the number of characters of the Binary (String Hex Mod) // to be converted, not including the terminating NULL character. If this // parameter is zero, wsBinary.length() will be used to get the cchString. // // RETURN: // Success: std::wstring that contains the formatted string // Failure: NULL (empty) std::wstring, For extended error information, call GetLastError. // // NOTE: // Remember that 4 characters of the Binary (String Hex Mod) = 1 characters of formatted String // so if wsBinary is less than 4 characters, BinaryToString return NULL (empty) std::wstring and set ERROR_INVALID_DATA // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// std::wstring BinaryToString(std::wstring& wsBinary, size_t cchString) { if (!cchString && !(cchString = wsBinary.length())) { SetLastError(ERROR_INVALID_DATA); return std::wstring(); } DWORD dwCount = (DWORD)(cchString > _HEAP_MAXREQ_X32 ? _HEAP_MAXREQ_X32 : cchString), cbBinary; //, pdwSkip, pdwFlags; //if (cchString > _HEAP_MAXREQ_X32) { SetLastError(ERROR_FILE_TOO_LARGE); } if (!CryptStringToBinaryW(wsBinary.c_str(), dwCount, CRYPT_STRING_HEXRAW, NULL, &cbBinary, NULL, NULL)) { return nullptr; } std::wstring* wsString; try { wsString = new /*(std::nothrow)*/ std::wstring(cbBinary / 2, wcNULL); } catch (const std::exception&) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } //if (!CryptStringToBinaryW(wsBinary.c_str(), dwCount, CRYPT_STRING_HEXRAW, (LPBYTE)const_cast<LPWSTR>(wsString->c_str()), &cbBinary, &pdwSkip, &pdwFlags)) { return std::wstring(); } if (!CryptStringToBinaryW(wsBinary.c_str(), dwCount, CRYPT_STRING_HEXRAW, (LPBYTE)const_cast<LPWSTR>(wsString->c_str()), &cbBinary, NULL, NULL)) { delete wsString; return std::wstring(); } return *wsString; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // _BinaryToString // The BinaryToString function converts a Binary (UTF16 Little Endian) variant into a formatted String. // // PARAMETERS: // wsBinary _In_ A std::wstring of bytes (String Hex Mod) to be converted into a string // cchString _In_ The Length of wsBinary, the number of characters of the Binary (String Hex Mod) // to be converted, not including the terminating NULL character. If this // parameter is zero, wsBinary.length() will be used to get the cchString. // // RETURN: // Success: Pointer of std::wstring that contains the formatted string // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Remember that 4 characters of the Binary (String Hex Mod) = 1 characters of formatted String // so if wsBinary is less than 4 characters, _BinaryToString return nullptr and set ERROR_INVALID_DATA // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// std::wstring* _BinaryToString(std::wstring& wsBinary, size_t cchString) { if (!cchString && !(cchString = wsBinary.length())) { SetLastError(ERROR_INVALID_DATA); return nullptr; } DWORD dwCount = (DWORD)(cchString > _HEAP_MAXREQ_X32 ? _HEAP_MAXREQ_X32 : cchString), cbBinary; //, pdwSkip, pdwFlags; //if (cchString > _HEAP_MAXREQ_X32) { SetLastError(ERROR_FILE_TOO_LARGE); } if (!CryptStringToBinaryW(wsBinary.c_str(), dwCount, CRYPT_STRING_HEXRAW, NULL, &cbBinary, NULL, NULL)) { return nullptr; } std::wstring* wsString; try { wsString = new /*(std::nothrow)*/ std::wstring(cbBinary / 2, wcNULL); } catch (const std::exception&) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } //if (!CryptStringToBinaryW(wsBinary.c_str(), dwCount, CRYPT_STRING_HEXRAW, (LPBYTE)const_cast<LPWSTR>(wsString->c_str()), &cbBinary, &pdwSkip, &pdwFlags)) { return std::wstring(); } if (!CryptStringToBinaryW(wsBinary.c_str(), dwCount, CRYPT_STRING_HEXRAW, (LPBYTE)const_cast<LPWSTR>(wsString->c_str()), &cbBinary, NULL, NULL)) { delete wsString; return nullptr; } return wsString; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // StringToBinaryEx // The StringToBinaryEx function converts a formatted string into an Binary (UTF16 Little Endian) String Hex Mod. // // PARAMETERS: // pszString _In_ A pointer to a string that contains the formatted string to be converted. // cchString _In_ The number of characters of the formatted string to be converted, not // including the terminating NULL character. If this parameter is zero, pszString // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of array of bytes (Binary UTF16 Little Endian, String Hex Mod) // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use delete[] Pointer; to release the memory // Remember that 1 characters of formatted String = 4 characters of the Binary (String Hex Mod) // so the limit of (cchString) characters to converted in _WIN32 is 0x1FFFFFFC (_HEAP_MAXREQ / sizeof(wchar_t) / 4) // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPWSTR StringToBinaryEx(LPWSTR pszString, size_t cchString) { if (!cchString && !(cchString = wcslen(pszString))) { SetLastError(ERROR_INVALID_DATA); return nullptr; } LPWSTR pszBinary, pwsBinary; size_t uCount = ((cchString > _MAX_RBS_ ? _RAW_BIN_STR_ : cchString) * 4) + (cchString < _RAW_BIN_STR_); if (!(pszBinary = new (std::nothrow) wchar_t[uCount])) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } pwsBinary = pszBinary; pszBinary[uCount - 1] = wcNULL; for (LPBYTE pbBinary = (LPBYTE)pszString; cchString != 0; --cchString, pwsBinary += 4, pbBinary += 2) { swprintf(pwsBinary, uCount, L"%02X%02X", *pbBinary, *(pbBinary + 1)); } return pszBinary; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // StringToBinaryEx // The StringToBinaryEx function converts a formatted string into an Binary (UTF16 Little Endian) String Hex Mod. // // PARAMETERS: // pszString _In_ A pointer to a string that contains the formatted string to be converted. // cchString _In_ The number of characters of the formatted string to be converted, not // including the terminating NULL character. If this parameter is zero, pszString // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of array of bytes (Binary UTF16 Little Endian, String Hex Mod) // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use delete[] Pointer; to release the memory // Remember that 1 characters of formatted String = 4 characters of the Binary (String Hex Mod) // so the limit of (cchString) characters to converted in _WIN32 is 0x1FFFFFFC (_HEAP_MAXREQ / sizeof(wchar_t) / 4) // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPCWSTR StringToBinaryEx(LPCWSTR pszString, size_t cchString) { if (!cchString && !(cchString = wcslen(pszString))) { SetLastError(ERROR_INVALID_DATA); return nullptr; } LPWSTR pszBinary, pwsBinary; size_t uCount = ((cchString > _MAX_RBS_ ? _RAW_BIN_STR_ : cchString) * 4) + (cchString < _RAW_BIN_STR_); if (!(pszBinary = new (std::nothrow) wchar_t[uCount])) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } pwsBinary = pszBinary; pszBinary[uCount - 1] = wcNULL; for (LPBYTE pbBinary = (LPBYTE)pszString; cchString != 0; --cchString, pwsBinary += 4, pbBinary += 2) { swprintf(pwsBinary, uCount, L"%02X%02X", *pbBinary, *(pbBinary + 1)); } return pszBinary; //const_cast<LPCWSTR>(pszBinary); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // _StringToBinaryEx // The StringToBinaryEx function converts a formatted string into an Binary (UTF16 Little Endian) String Hex Mod. // // PARAMETERS: // pszString _In_ A pointer to a string that contains the formatted string to be converted. // cchString _In_ The number of characters of the formatted string to be converted, not // including the terminating NULL character. If this parameter is zero, pszString // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of array of bytes (Binary UTF16 Little Endian, String Hex Mod) // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use free(Pointer); to release the memory // Remember that 1 characters of formatted String = 4 characters of the Binary (String Hex Mod) // so the limit of (cchString) characters to converted in _WIN32 is 0x1FFFFFFC (_HEAP_MAXREQ / sizeof(wchar_t) / 4) // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPWSTR _StringToBinaryEx(LPWSTR pszString, size_t cchString) { if (!cchString && !(cchString = wcslen(pszString))) { SetLastError(ERROR_INVALID_DATA); return nullptr; } LPWSTR pszBinary, pwsBinary; size_t uCount = ((cchString > _MAX_RBS_ ? _RAW_BIN_STR_ : cchString) * 4) + (cchString < _RAW_BIN_STR_); if (!(pszBinary = (LPWSTR)malloc(uCount * sizeof(wchar_t)))) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } LPBYTE pbBinary = (LPBYTE)pszString; pwsBinary = pszBinary; pszBinary[uCount - 1] = wcNULL; do { swprintf(pwsBinary, uCount, L"%02X%02X", *pbBinary, *(pbBinary + 1)); pwsBinary += 4; pbBinary += 2; } while (--cchString); return pszBinary; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // _StringToBinaryEx // The StringToBinaryEx function converts a formatted string into an Binary (UTF16 Little Endian) String Hex Mod. // // PARAMETERS: // pszString _In_ A pointer to a string that contains the formatted string to be converted. // cchString _In_ The number of characters of the formatted string to be converted, not // including the terminating NULL character. If this parameter is zero, pszString // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of array of bytes (Binary UTF16 Little Endian, String Hex Mod) // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use free(Pointer); to release the memory // Remember that 1 characters of formatted String = 4 characters of the Binary (String Hex Mod) // so the limit of (cchString) characters to converted in _WIN32 is 0x1FFFFFFC (_HEAP_MAXREQ / sizeof(wchar_t) / 4) // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPCWSTR _StringToBinaryEx(LPCWSTR pszString, size_t cchString) { if (!cchString && !(cchString = wcslen(pszString))) { SetLastError(ERROR_INVALID_DATA); return nullptr; } LPWSTR pszBinary, pwsBinary; size_t uCount = ((cchString > _MAX_RBS_ ? _RAW_BIN_STR_ : cchString) * 4) + (cchString < _RAW_BIN_STR_); if (!(pszBinary = (LPWSTR)malloc(uCount * sizeof(wchar_t)))) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } LPBYTE pbBinary = (LPBYTE)pszString; pwsBinary = pszBinary; pszBinary[uCount - 1] = wcNULL; do { swprintf(pwsBinary, uCount, L"%02X%02X", *pbBinary, *(pbBinary + 1)); pwsBinary += 4; pbBinary += 2; } while (--cchString); return pszBinary; //const_cast<LPCWSTR>(pszBinary); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // StringToBinaryEx // The StringToBinaryEx function converts a formatted string into an Binary (UTF16 Little Endian). // // PARAMETERS: // wsString _In_ A std::wstring that contains the formatted string to be converted // cchString _In_ The Length of wsString, the number of characters of the formatted string // to be converted, not including the terminating NULL character. If this // parameter is zero, wsString.length() will be used to get the cchString. // // RETURN: // Success: std::wstring that contains the Binary UTF16 Little Endian (String Hex Mod) // Failure: NULL (empty) std::wstring, For extended error information, call GetLastError. // // NOTE: // Remember that 1 characters of formatted String = 4 characters of the Binary (String Hex Mod) // so the limit of (cchString) characters to converted in _WIN32 is 0x1FFFFFFC (_HEAP_MAXREQ / sizeof(wchar_t) / 4) // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// std::wstring StringToBinaryEx(std::wstring& wsString, size_t cchString) { if (!cchString && !(cchString = wsString.length())) { SetLastError(ERROR_INVALID_DATA); return std::wstring(); } std::wstring* wsBinary; size_t i = 0, uCount = ((cchString > _MAX_RBS_ ? _RAW_BIN_STR_ : cchString) * 4) + (cchString < _RAW_BIN_STR_); try { wsBinary = new /*(std::nothrow)*/ std::wstring(uCount - 1, wcNULL); } catch (const std::exception&) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return std::wstring(); } LPWSTR pszBinary = const_cast<LPWSTR>(wsBinary->c_str()); for (LPCBYTE pbBinary = (LPCBYTE)wsString.c_str(); i < cchString; ++i, pbBinary += 2, pszBinary += 4) { swprintf(pszBinary, uCount, L"%02X%02X", *pbBinary, *(pbBinary + 1)); } return *wsBinary; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // _StringToBinaryEx // The StringToBinaryEx function converts a formatted string into an Binary (UTF16 Little Endian). // // PARAMETERS: // wsString _In_ A std::wstring that contains the formatted string to be converted // cchString _In_ The Length of wsString, the number of characters of the formatted string // to be converted, not including the terminating NULL character. If this // parameter is zero, wsString.length() will be used to get the cchString. // // RETURN: // Success: Pointer of std::wstring that contains the Binary UTF16 Little Endian (String Hex Mod) // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Remember that 1 characters of formatted String = 4 characters of the Binary (String Hex Mod) // so the limit of (cchString) characters to converted in _WIN32 is 0x1FFFFFFC (_HEAP_MAXREQ / sizeof(wchar_t) / 4) // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// std::wstring* _StringToBinaryEx(std::wstring& wsString, size_t cchString) { if (!cchString && !(cchString = wsString.length())) { SetLastError(ERROR_INVALID_DATA); return nullptr; } std::wstring* wsBinary; size_t i = 0, uCount = ((cchString > _MAX_RBS_ ? _RAW_BIN_STR_ : cchString) * 4) + (cchString < _RAW_BIN_STR_); try { wsBinary = new /*(std::nothrow)*/ std::wstring(uCount - 1, wcNULL); } catch (const std::exception&) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } LPWSTR pszBinary = const_cast<LPWSTR>(wsBinary->c_str()); for (LPCBYTE pbBinary = (LPCBYTE)wsString.c_str(); i < cchString; ++i, pbBinary += 2, pszBinary += 4) { swprintf(pszBinary, uCount, L"%02X%02X", *pbBinary, *(pbBinary + 1)); } return wsBinary; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // StringToBinary // The StringToBinary function converts a formatted string into an Binary (UTF16 Little Endian) String Hex Mod. // // PARAMETERS: // pszString _In_ A pointer to a string that contains the formatted string to be converted. // cchString _In_ The number of characters of the formatted string to be converted, not // including the terminating NULL character. If this parameter is zero, pszString // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of array of bytes (Binary UTF16 Little Endian, String Hex Mod) // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use delete[] Pointer; to release the memory // Remember that 1 characters of formatted String = 4 characters of the Binary (String Hex Mod) // so the limit of (cchString) characters to converted in _WIN32 is 0x1FFFFFFC (_HEAP_MAXREQ / sizeof(wchar_t) / 4) // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPWSTR StringToBinary(LPWSTR pszString, size_t cchString) { if (!cchString && !(cchString = wcslen(pszString))) { SetLastError(ERROR_INVALID_DATA); return nullptr; } DWORD cbBinary = (DWORD)((cchString > _MAX_RBS_X32 ? _RAW_BIN_STR_X32 : cchString) * sizeof(wchar_t)) + (cchString < _RAW_BIN_STR_X32), dwCount = 0; LPWSTR pszBinary; LPCBYTE pbBinary = (LPBYTE)pszString; if (!CryptBinaryToStringW(pbBinary, cbBinary, CRYPT_STRING_HEXRAW_NOCRLF, NULL, &dwCount)) { return nullptr; } else if (!(pszBinary = new (std::nothrow) wchar_t[dwCount])) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } pszBinary[dwCount - 1] = wcNULL; if (!CryptBinaryToStringW(pbBinary, cbBinary, CRYPT_STRING_HEXRAW_NOCRLF, pszBinary, &dwCount)) { delete[] pszBinary; return nullptr; } for (LPWSTR pBinary = pszBinary; dwCount != 0; --dwCount, ++pBinary) { *pBinary = towupper(*pBinary); } return pszBinary; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // StringToBinary // The StringToBinary function converts a formatted string into an Binary (UTF16 Little Endian) String Hex Mod. // // PARAMETERS: // pszString _In_ A pointer to a string that contains the formatted string to be converted. // cchString _In_ The number of characters of the formatted string to be converted, not // including the terminating NULL character. If this parameter is zero, pszString // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of array of bytes (Binary UTF16 Little Endian, String Hex Mod) // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use delete[] Pointer; to release the memory // Remember that 1 characters of formatted String = 4 characters of the Binary (String Hex Mod) // so the limit of (cchString) characters to converted in _WIN32 is 0x1FFFFFFC (_HEAP_MAXREQ / sizeof(wchar_t) / 4) // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPCWSTR StringToBinary(LPCWSTR pszString, size_t cchString) { if (!cchString && !(cchString = wcslen(pszString))) { SetLastError(ERROR_INVALID_DATA); return nullptr; } DWORD cbBinary = (DWORD)((cchString > _MAX_RBS_X32 ? _RAW_BIN_STR_X32 : cchString) * sizeof(wchar_t)) + (cchString < _RAW_BIN_STR_X32), dwCount = 0; LPWSTR pszBinary; LPCBYTE pbBinary = (LPBYTE)pszString; if (!CryptBinaryToStringW(pbBinary, cbBinary, CRYPT_STRING_HEXRAW_NOCRLF, NULL, &dwCount)) { return nullptr; } else if (!(pszBinary = new (std::nothrow) wchar_t[dwCount])) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } pszBinary[dwCount - 1] = wcNULL; if (!CryptBinaryToStringW(pbBinary, cbBinary, CRYPT_STRING_HEXRAW_NOCRLF, pszBinary, &dwCount)) { delete[] pszBinary; return nullptr; } for (LPWSTR pBinary = pszBinary; dwCount != 0; --dwCount, ++pBinary) { *pBinary = towupper(*pBinary); } return pszBinary; //const_cast<LPCWSTR>(pszBinary); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // _StringToBinary // The StringToBinary function converts a formatted string into an Binary (UTF16 Little Endian) String Hex Mod. // // PARAMETERS: // pszString _In_ A pointer to a string that contains the formatted string to be converted. // cchString _In_ The number of characters of the formatted string to be converted, not // including the terminating NULL character. If this parameter is zero, pszString // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of array of bytes (Binary UTF16 Little Endian, String Hex Mod) // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use free(Pointer); to release the memory // Remember that 1 characters of formatted String = 4 characters of the Binary (String Hex Mod) // so the limit of (cchString) characters to converted in _WIN32 is 0x1FFFFFFC (_HEAP_MAXREQ / sizeof(wchar_t) / 4) // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPWSTR _StringToBinary(LPWSTR pszString, size_t cchString) { if (!cchString && !(cchString = wcslen(pszString))) { SetLastError(ERROR_INVALID_DATA); return nullptr; } DWORD cbBinary = (DWORD)((cchString > _MAX_RBS_X32 ? _RAW_BIN_STR_X32 : cchString) * sizeof(wchar_t)) + (cchString < _RAW_BIN_STR_X32), dwCount = 0; LPWSTR pszBinary; LPCBYTE pbBinary = (LPBYTE)pszString; if (!CryptBinaryToStringW(pbBinary, cbBinary, CRYPT_STRING_HEXRAW_NOCRLF, NULL, &dwCount)) { return nullptr; } else if (!(pszBinary = (LPWSTR)malloc(dwCount * sizeof(wchar_t)))) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } pszBinary[dwCount - 1] = wcNULL; if (!CryptBinaryToStringW(pbBinary, cbBinary, CRYPT_STRING_HEXRAW_NOCRLF, pszBinary, &dwCount)) { free(pszBinary); return nullptr; } for (LPWSTR pBinary = pszBinary; dwCount != 0; --dwCount, ++pBinary) { *pBinary = towupper(*pBinary); } return pszBinary; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // _StringToBinary // The StringToBinary function converts a formatted string into an Binary (UTF16 Little Endian) String Hex Mod. // // PARAMETERS: // pszString _In_ A pointer to a string that contains the formatted string to be converted. // cchString _In_ The number of characters of the formatted string to be converted, not // including the terminating NULL character. If this parameter is zero, pszString // is considered to be a null-terminated string. // // RETURN: // Success: Pointer of array of bytes (Binary UTF16 Little Endian, String Hex Mod) // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Use free(Pointer); to release the memory // Remember that 1 characters of formatted String = 4 characters of the Binary (String Hex Mod) // so the limit of (cchString) characters to converted in _WIN32 is 0x1FFFFFFC (_HEAP_MAXREQ / sizeof(wchar_t) / 4) // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// LPCWSTR _StringToBinary(LPCWSTR pszString, size_t cchString) { if (!cchString && !(cchString = wcslen(pszString))) { SetLastError(ERROR_INVALID_DATA); return nullptr; } DWORD cbBinary = (DWORD)((cchString > _MAX_RBS_X32 ? _RAW_BIN_STR_X32 : cchString) * sizeof(wchar_t)) + (cchString < _RAW_BIN_STR_X32), dwCount = 0; LPWSTR pszBinary; LPCBYTE pbBinary = (LPBYTE)pszString; if (!CryptBinaryToStringW(pbBinary, cbBinary, CRYPT_STRING_HEXRAW_NOCRLF, NULL, &dwCount)) { return nullptr; } else if (!(pszBinary = (LPWSTR)malloc(dwCount * sizeof(wchar_t)))) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } pszBinary[dwCount - 1] = wcNULL; if (!CryptBinaryToStringW(pbBinary, cbBinary, CRYPT_STRING_HEXRAW_NOCRLF, pszBinary, &dwCount)) { free(pszBinary); return nullptr; } for (LPWSTR pBinary = pszBinary; dwCount != 0; --dwCount, ++pBinary) { *pBinary = towupper(*pBinary); } return pszBinary; //const_cast<LPCWSTR>(pszBinary); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // StringToBinaryEx // The StringToBinaryEx function converts a formatted string into an Binary (UTF16 Little Endian). // // PARAMETERS: // wsString _In_ A std::wstring that contains the formatted string to be converted // cchString _In_ The Length of wsString, the number of characters of the formatted string // to be converted, not including the terminating NULL character. If this // parameter is zero, wsString.length() will be used to get the cchString. // // RETURN: // Success: std::wstring that contains the Binary UTF16 Little Endian (String Hex Mod) // Failure: NULL (empty) std::wstring, For extended error information, call GetLastError. // // NOTE: // Remember that 1 characters of formatted String = 4 characters of the Binary (String Hex Mod) // so the limit of (cchString) characters to converted in _WIN32 is 0x1FFFFFFC (_HEAP_MAXREQ / sizeof(wchar_t) / 4) // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// std::wstring StringToBinary(std::wstring& wsString, size_t cchString) { if (!cchString && !(cchString = wsString.length())) { SetLastError(ERROR_INVALID_DATA); return std::wstring(); } DWORD cbBinary = (DWORD)((cchString > _MAX_RBS_X32 ? _RAW_BIN_STR_X32 : cchString) * sizeof(wchar_t)), dwCount = 0; LPCBYTE pbBinary = (LPCBYTE)wsString.c_str(); if (!CryptBinaryToStringW(pbBinary, cbBinary, CRYPT_STRING_HEXRAW_NOCRLF, NULL, &dwCount)) { return std::wstring(); } std::wstring* wsBinary; try { wsBinary = new /*(std::nothrow)*/ std::wstring(dwCount, wcNULL); } catch (const std::exception&) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return std::wstring(); } if (!CryptBinaryToStringW(pbBinary, cbBinary, CRYPT_STRING_HEXRAW_NOCRLF, const_cast<LPWSTR>(wsBinary->c_str()), &dwCount)) { delete wsBinary; return std::wstring(); } for (std::wstring::iterator it = wsBinary->begin(); it != wsBinary->end(); ++it) { *it = towupper(*it); } return *wsBinary; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // _StringToBinaryEx // The StringToBinaryEx function converts a formatted string into an Binary (UTF16 Little Endian). // // PARAMETERS: // wsString _In_ A std::wstring that contains the formatted string to be converted // cchString _In_ The Length of wsString, the number of characters of the formatted string // to be converted, not including the terminating NULL character. If this // parameter is zero, wsString.length() will be used to get the cchString. // // RETURN: // Success: Pointer of std::wstring that contains the Binary UTF16 Little Endian (String Hex Mod) // Failure: Null Pointer, For extended error information, call GetLastError. // // NOTE: // Remember that 1 characters of formatted String = 4 characters of the Binary (String Hex Mod) // so the limit of (cchString) characters to converted in _WIN32 is 0x1FFFFFFC (_HEAP_MAXREQ / sizeof(wchar_t) / 4) // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// std::wstring* _StringToBinary(std::wstring& wsString, size_t cchString) { if (!cchString && !(cchString = wsString.length())) { SetLastError(ERROR_INVALID_DATA); return nullptr; } DWORD cbBinary = (DWORD)((cchString > _MAX_RBS_X32 ? _RAW_BIN_STR_X32 : cchString) * sizeof(wchar_t)), dwCount = 0; LPCBYTE pbBinary = (LPCBYTE)wsString.c_str(); if (!CryptBinaryToStringW(pbBinary, cbBinary, CRYPT_STRING_HEXRAW_NOCRLF, NULL, &dwCount)) { return nullptr; } std::wstring* wsBinary; try { wsBinary = new /*(std::nothrow)*/ std::wstring(dwCount, wcNULL); } catch (const std::exception&) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return nullptr; } if (!CryptBinaryToStringW(pbBinary, cbBinary, CRYPT_STRING_HEXRAW_NOCRLF, const_cast<LPWSTR>(wsBinary->c_str()), &dwCount)) { delete wsBinary; return nullptr; } for (std::wstring::iterator it = wsBinary->begin(); it != wsBinary->end(); ++it) { *it = towupper(*it); } return wsBinary; } however if for some reason you want to keep the format you asked for in the first post, the fast way would be the MsgBox(0, "ANSI Mod", StringTrimLeft(StringRegExpReplace(StringToBinary("Test"), "..\K(?!$)", "\\x"), 2)) MsgBox(0, "Recommended - Unicode Mod", StringRegExpReplace(StringTrimLeft(StringToBinary("测试", 2), 2), "....", "\\x$0")) Ciao. PS: to be more portable ignores func that using the new + delete[], always use func that using the malloc and free Edited January 23, 2015 by DXRW4E Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now