openframeworks / openframeworks/openFrameworks

Extend ofToString to convert Windows data types

Open
#4,098 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

core feature section-internals
Dominant language
C++
Stars
10.4k
Forks
2.6k
Avg merge
1d 21h
Merged PRs (30d)
9

Description

it doesn't happen often, but often enough that the feature would be nice to have, where I need to convert w_chars, LPTSTR, LPTWSTR etc... the functions are already a part of ofSystemUtils

std::string convertWideToNarrow(const wchar_t *s, char dfault = '?',
const std::locale& loc = std::locale())
{
  std::ostringstream stm;

  while( *s != L'\0' ) {
    stm << std::use_facet< std::ctype<wchar_t> >( loc ).narrow( *s++, dfault );
  }
  return stm.str();
}

std::wstring convertNarrowToWide( const std::string& as ){
    // deal with trivial case of empty string
    if( as.empty() )    return std::wstring();

    // determine required length of new string
    size_t reqLength = ::MultiByteToWideChar( CP_UTF8, 0, as.c_str(), (int)as.length(), 0, 0 );

   // construct new string of required length
   std::wstring ret( reqLength, L'\0' );

   // convert old string to new string
    ::MultiByteToWideChar( CP_UTF8, 0, as.c_str(), (int)as.length(), &ret[0], (int)ret.length() );

    // return new string ( compiler should optimize this away )
    return ret;
}

The problem when you do ofToString(LPTSTR...) is you get the pointer not the string itself e.g.

LPTSTR errorText = 0x0336ffa0 L"The media session cannot pause from a stopped state." wchar_t *

string error = "0336FFA0" std::basic_string<char,std::char_traits,std::allocator >

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the ofSystemUtils conversion functions and the existing ofToString overloads first, focusing on how Windows types such as wchar_t pointers, LPTSTR, and LPTWSTR are represented. Done means these values produce their string contents rather than pointer addresses, while preserving behavior on other platforms.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
operating-systems
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.