title: UTF-8 to UCS2 conversion using icu resulting in gibberish
body: <p>This is a follow up question to <a href="https://stackoverflow.com/questions/22209841/utf-8-to-ucs-2-conversion-with-icu-library">a previous one</a></p>
  
  <p>The issue in that question has been resolved, and now the code is proceeding as expected, however the final output of the utf-8 to ucs2 conversion is gibberish. By that I mean the hex values of the final text don't correspond in anyway to the utf-8 version. I know they are different encodings, but there doesn't seem to be any mapping between the two.</p>
  
  <p>The input into the conversion is "ĩ", the output is "ÿþ)^A". In hex the values are c4a9 for "ĩ" (utf-8 value) and "00FF 00FE 0029 0001" for "ÿþ)^A" (ucs2 values).</p>
  
  <p>I'm hoping someone has an explanation for this behavior or can tell me what I've done incorrectly in the code.</p>
  
  <p>The new updated code is:</p>
  
  <pre><code>UErrorCode resultCode = U_ZERO_ERROR;
  
  UConverter* pLatinOneConv = ucnv_open("ISO-8859-1", &amp;resultCode);
  
  // Change the callback to error out instead of the default            
  const void* oldContext;
  UConverterFromUCallback oldFromAction;
  UConverterToUCallback oldToAction;
  ucnv_setFromUCallBack(pLatinOneConv, UCNV_FROU_CALLBACK_STOP, NULL, &amp;oldFromAction, &amp;oldContext, &amp;resultCode);
  ucnv_setToUCallBack(pLatinOneConv, UCNV_TO_U_CALLBACK_STOP, NULL, &amp;oldToAction, &amp;oldContext, &amp;resultCode);
  
  int32_t outputLength = 0;
  int bodySize = uniString.length();
  int targetSize = bodySize * 4;
  char* target = new char[targetSize];                       
  
  printf("Body: %s\n", uniString.c_str());
  if (U_SUCCESS(resultCode))
  {
      outputLength = ucnv_fromAlgorithmic(pLatinOneConv, UCNV_UTF8, target, targetSize, uniString.c_str(),
          uniString.length(), &amp;resultCode);
      ucnv_close(pLatinOneConv);
  }
  printf("ISO-8859-1 just tried to convert '%s' to '%s' with error '%i' and length '%i'", uniString.c_str(), 
      outputLength ? target : "invalid_char", resultCode, outputLength);
  
  if (resultCode == U_INVALID_CHAR_FOUND || resultCode == U_ILLEGAL_CHAR_FOUND || resultCode == U_TRUNCATED_CHAR_FOUND)
  {
      if (resultCode == U_INVALID_CHAR_FOUND)
      {
          resultCode = U_ZERO_ERROR;
          printf("Unmapped input character, cannot be converted to Latin1");                    
          // segment Text, if necessary, and add UUIDs copy existing pPdu's addresses and optionals
          UConverter* pUscTwoConv = ucnv_open("UCS-2", &amp;resultCode);
          if (U_SUCCESS(resultCode))
          {
              printf("Text Body: %s\n", uniString.c_str());
              outputLength = ucnv_fromAlgorithmic(pUscTwoConv, UCNV_UTF8, target, targetSize, uniString.c_str(),
                  uniString.length(), &amp;resultCode);
              ucnv_close(pUscTwoConv);
          }
          printf("UCS-2 just tried to convert '%s' to '%s' with error '%i' and length '%i'", uniString.c_str(), 
              outputLength ? target : "invalid_char", resultCode, outputLength);
  
          if (U_SUCCESS(resultCode))
          {
              pdus = SegmentText(target, pPdu, SEGMENT_SIZE_UNICODE_MAX, true);
          }
      }
      else
      {
          printf("DecodeText(): Text contents does not appear to be valid UTF-8");
      }
  }
  else
  {
      printf("DecodeText(): Text successfully converted to Latin1");
      std::string newBody(target, outputLength);
      pdus = SegmentText(newBody, pPdu, SEGMENT_SIZE_MAX);
  }
  </code></pre>