[Question]
  [
Given a string, character list, byte stream, sequence… which is both valid UTF-8 and valid Windows-1252 (most languages will probably want to take a normal UTF-8 string), [convert it](https://en.wikipedia.org/wiki/Mojibake "Mojibake - Wikipedia") from (that is, *pretend* it is) [Windows-1252](https://en.wikipedia.org/wiki/Windows-1252 "Windows-1252 - Wikipedia") to [UTF-8](https://en.wikipedia.org/wiki/UTF-8 "UTF-8 - Wikipedia").
### Walked-through example
The UTF-8 string  
`I`       `♥`       `U`  `T`  `F`  `-`  `8`  
is represented as the bytes  
`49` `20`  `E2 99 A5`  `20` `55` `54` `46` `2D` `38`  
these byte values in [the Windows-1252 table](https://en.wikipedia.org/wiki/Windows-1252#Character_set) gives us the Unicode equivalents  
`49` `20` `E2 2122 A5` `20` `55` `54` `46` `2D` `38`  
which render as  
`I`    `â`  `™`  `¥`    `U`  `T`  `F`  `-`  `8`
### Examples
`£` → `Â£`
`Â£` → `Ã‚Â£`
`Ã‚Â£` → `Ãƒâ€šÃ‚Â£`
`I ♥ UTF-8` → `I â™¥ UTF-8`
`árvíztűrő tükörfúrógép` → `Ã¡rvÃztÅ±rÅ‘ tÃ¼kÃ¶rfÃºrÃ³gÃ©p`
  
[Answer]
## bash, 14 bytes
```
iconv -fCP1252
```
[Try it online!](https://tio.run/##S0oszvj/PzM5P69MQTfNOcDQyNTo//9Di7kON4GI5kcNs8AsT4VHM5cqhIa46VpwHV5YVHZ4bVXJ0Y1FRycqlBzek314W1Ha4V1FhzenH15ZAAA)
[Answer]
# Java 8, ~~72~~ ~~66~~ ~~36~~ 25 bytes
```
s->new String(s,"cp1252")
```
[Try it online.](https://tio.run/##lY8xS8NAFMf3fIpHpgTsgQVBCDoIFRzsop3E4Xq5xkvTu@PuJbVKQPQTOOom4uDQQQQHcfLoF@kXiVfMJghZHjz4/X/v/3Ja0V6eThtWUGvhmAp5HQAIidxMKOMw3KwAJ2iEzIBF4wXys3OwMeCFUXMLuTcQochI2lJrZZCnA8lU6vHBJeMahZKJd9SBHxYpCgZDkLAHje3tSz5v3ZHdCpne7u/0w7hJNrAux4WH20ylRAoz3y/65X0J2qlE@8jCIp8RVSLRXoOFjCRhUfj9HJKM44F/z0ZxHCf/0u62I3@3vnnsGjqC9cMLjE4Pe7udbj2Zyi2vcPVmVveA7mvqPszEfRr3nrlX/VdVB3XzAw)
```
s->  // Method with byte-array (UTF-8 by default) as parameter and String return-type
  new String(s,"cp1252")
     //  Pretend this UTF-8 input is (and convert it to) Windows-1252,
     //  and return it as UTF-8 String (by default) as well
```
`cp1252` is an alias for `Windows-1252`. This alias `cp1252` is the Canonical Name for the `java.io` and `java.lang` APIs, while the full name `Windows-1252` is the Canonical Name for the `java.nio` API. [See here for a full list of supported Java encodings](https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html), where we'd always want to use the shortest of the two for codegolfing.
[Answer]
# [R](https://www.r-project.org/) 3.5.0 or higher, ~~32~~ 20 bytes
```
scan(,"",e="latin1")
```
[Try it online!](https://tio.run/##AToAxf9y//9zY2FuKCwiIixlPSJsYXRpbjEiKf8iw6FydsOtenTFsXLFkSB0w7xrw7ZyZsO6csOzZ8OpcCL/ "R – Try It Online")
Oddly short for a [string](/questions/tagged/string "show questions tagged 'string'") challenge in R... *thanks to JayCe for golfing down 12 more bytes!*
`scan` optionally takes an `encoding` argument to set the encoding of the input string. `latin1` corresponds to, according to the documentation of [`Encoding`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Encoding.html)
> 
> There is some ambiguity as to what is meant by a ‘Latin-1’ locale, since some OSes (notably Windows) make use of character positions used for control characters in the ISO 8859-1 character set. How such characters are interpreted is system-dependent but as from R 3.5.0 they are if possible interpreted as per Windows codepage 1252 (which Microsoft calls ‘Windows Latin 1 (ANSI)’) when converting to e.g. UTF-8.
> 
> 
> 
[Answer]
# [Python 2](https://docs.python.org/2/), ~~40~~ 38 bytes
-2 bytes thanks to [Erik the Outgolfer](https://codegolf.stackexchange.com/users/41024/erik-the-outgolfer).
```
lambda s:s.decode('1252').encode('u8')
```
[Try it online!](https://tio.run/##K6gsycjPM/qvnJyfkpmXblVqwZVmG/M/JzE3KSVRodiqWC8lFSiVqqFuaGRqpK6pl5oH4ZZaqGv@T8svUihWyMxTUD@0WF1HQf1wE5RuftQwC8bxVHg0c6lCaIibrgVYbmFR2eG1VSVHNxYdnahQcnhP9uFtRWmHdxUd3px@eGWBuhWXgkJBUWZeiUIxUPWjtklATWkaxZr/AQ "Python 2 – Try It Online")
u8 is an alias for `utf-8`.
[Answer]
# [Ruby](https://www.ruby-lang.org/), 31 bytes
```
->s{s.encode("UTF-8","CP1252")}
```
[Try it online!](https://tio.run/##KypNqvyfZvtf1664ulgvNS85PyVVQyk0xE3XQklHyTnA0MjUSEmz9n9BaUmxQrTSocVKOlxKh5ugdPOjhlkwjqfCo5lLFaA6gXILi8oOr60qObqx6OhEhZLDe7IPbytKO7yr6PDm9MMrC5Ri9XITC6primvSootja7WUYvKU/iMZAQA "Ruby – Try It Online")
Tests cases are included in the TIO
[Answer]
# [Python 3](https://docs.python.org/3/), ~~38 36~~ 34 bytes
```
lambda s:s.encode().decode('1252')
```
[Try it online!](https://tio.run/##K6gsycjPM/6vnJyfkpmXblVqwZVmG/M/JzE3KSVRodiqWC81DyiVqqGpl5IKZqgbGpkaqWv@T8svUihWyMxTiFY/tFhdR0H9cBOUbn7UMAvG8VR4NHOpQmiIm64FWG5hUdnhtVUlRzcWHZ2oUHJ4T/bhbUVph3cVHd6cfnhlgXqsFZcCEBQUZeaVaBQDNTxqmwTUl6ZRrKn5HwA "Python 3 – Try It Online")
*note: After I had a working function I used [ovs's python2 answer](https://codegolf.stackexchange.com/a/167246/79918) answer to learn about the header and footer fields for tio, so the header and footer are the same*
*edit: Trimmed it a little thanks to python3 defaulting to utf8 and a tip from ovs's submission :)*
[Answer]
# C#, 81 bytes
```
using e=System.Text.Encoding;s=>e.GetEncoding(1252).GetString(e.UTF8.GetBytes(s))
```
[Try it online!](https://tio.run/##hU29SgNBEO73KYZUe0UWEhAC4Sz8RdAqEevzbu5YsrcbdibBKHkIS0sLCwsLESzEbsmL5EXOPXOxdZqZ72e@L6d@7axrmgVpW8FkRYz1WOwQpjuspnjH6tTmroj0WIgCDVYZIxD71niCRnYnJVGfL26NziE3GRHUmbbwICBOxxNnHNfS6QKuoiqTTm8nZkEJKVB6iOoceV8rB8ODYdIyk98miep6ejZqiaMVI0lKYvU@5dhZcgbVjdeMl9qiLGXvArZPLxC/@qPev@bw7Jfh7Z43737zCBy@Z@HTl@HLh48qvM7/AtZi3TQ/ "C# (Mono C# compiler) – Try It Online")
Thanks to Schmalls for 3 bytes
[Answer]
# 180 bytes, machine code (16-bit x86)
I noticed most answers use builtin encode/decode (which I believe is perfectly fine), but I thought I'll continue [my 16-bit quest](https://codegolf.stackexchange.com/a/180194/74387).
As with previous ones, this was done **without compiler** using mostly [HT hexeditor](http://hte.sourceforge.net) and [ICY's hexplorer](https://sourceforge.net/projects/hexplorer/).
```
00000000: eb40 ac20 0000 1a20 9201 1e20 2620 2020  .@. ... ... &                     
00000010: 2120 c602 3020 6001 3920 5201 0000 7d01  ! ..0 `.9 R...}.                  
00000020: 0000 0000 1820 1920 1c20 1d20 2220 1320  ..... . . . " .                   
00000030: 1420 dc02 2221 6101 3a20 5301 0000 7e01  . .."!a.: S...~.                  
00000040: 7801 89f7 4646 89fa 89d9 4143 4bb4 3fcd  x...FF....ACK.?.                  
00000050: 2185 c074 288a 053c 8073 05e8 1700 ebec  !..t(..<.s......                  
00000060: 3ca0 721a d440 0d80 c050 86c4 e806 0058  <[[email protected]](/cdn-cgi/l/email-protection)                  
00000070: e802 00eb d7b4 4088 05b3 01cd 21c3 2c80  ......@.....!.,.                  
00000080: d0e0 89c3 8b00 89cb 85c0 74c0 3dff 0773  ..........t.=..s                  
00000090: 08c1 c002 c0e8 02eb cd50 c1e8 0c0c e0e8  .........P......                  
000000a0: d3ff 5825 ff0f c1c0 02c0 e802 0d80 8050  ..X%...........P                  
000000b0: 86c4 ebb8                                ....                              
```
[![bake.com < input.txt > out.dat](https://i.stack.imgur.com/wOX9W.png)](https://i.stack.imgur.com/wOX9W.png)
## Dissection
Implementation is pretty straight-forward, although I haven't given much thought to flow upfront so there is SOME spaghetti there.
I'll mix order a bit, to make it easier to follow...
```
0000 eb40               jmp         0x42
```
Skip over table that maps chars >= 0x80 < 0xa0, to unicode codes.
```
data db ACh,20h, 00h,00h, 1Ah,20h, ...
```
Invalid ones are encoded as 0, they are not mapped to anything
```
0075 b440               mov         ah, 0x40   
0077 8805               mov         [di], al   
0079 b301               mov         bl, 0x1    
007b cd21               int         0x21       
007d c3                 ret                    
```
Helper function used to print char in `al`, will be called few times.
```
0042 89f7               mov         di, si     
0044 46                 inc         si         
0045 46                 inc         si         
0046 89fa               mov         dx, di     
0048 89d9               mov         cx, bx     
004a 41                 inc         cx         
004b 43                 inc         bx         
```
Prepare registers. Data will be read into 0x100, let `si` point into translation table above.
```
004c 4b                 dec         bx         
004d b43f               mov         ah, 0x3f   
004f cd21               int         0x21       
0051 85c0               test        ax, ax     
0053 7428               jz          0x7d       
```
Read char from stdin, jump to 0x7d if EOF.
Sidenote: This actually is a small (but pretty well known) trick, 0x7d contains `ret`,
this will cause `pop sp`, `sp` at start points to end of a segment, there's `00 00` there, and `cs:0` in DOS contains `CD 20`, which causes application to exit.
```
0055 8a05               mov         al, [di]   
0057 3c80               cmp         al, 0x80   
0059 7305               jnc         0x60       
005b e81700             call        0x75       
005e ebec               jmp         0x4c       
```
If char is < 0x80, just print it out, and go to beginning of loop
(because helper function is setting BX to 1 - stdout, jumps will go to `dec bx`)
```
0060 3ca0               cmp         al, 0xa0   
0062 721a               jc          0x7e       
0064 d440               aam         0x40       
0066 0d80c0             or          ax, c080   
0069 50                 push        ax         
006a 86c4               xchg        ah, al     
006c e80600             call        0x75       
006f 58                 pop         ax         
0070 e80200             call        0x75       
0073 ebd7               jmp         0x4c       
```
This part deals with chars >= 0xa0, splits ascii code into "high" two bits and "low" 6 bits and applies utf-8 mask c080 for two bytes, then prints both of them
```
007e 2c80               sub         al, 0x80   
0080 d0e0               shl         al, 0x1    
0082 89c3               mov         bx, ax     
0084 8b00               mov         ax, [bx+si]
0086 89cb               mov         bx, cx     
0088 85c0               test        ax, ax     
008a 74c0               jz          0x4c       
008c 3dff07             cmp         ax, 07ff   
008f 7308               jnc         0x99       
0091 c1c002             rol         ax, 0x2    
0094 c0e802             shr         al, 0x2    
0097 ebcd               jmp         0x66       
```
This part deals with chars >= 0x80 < 0xa0, it finds proper utf-8 code in the table at the top, if code equals 0, just skip to beginning, if it's below 0x7ff (ergo: fits on two UTF-8 bytes), just adjust the value and re-use previous code at 0x166.
```
0099 50                 push        ax         
009a c1e80c             shr         ax, 0xc    
009d 0ce0               or          al, e0     
009f e8d3ff             call        0x75       
00a2 58                 pop         ax         
00a3 25ff0f             and         ax, 0fff   
00a6 c1c002             rol         ax, 0x2    
00a9 c0e802             shr         al, 0x2    
00ac 0d8080             or          ax, 8080   
00af 50                 push        ax         
00b0 86c4               xchg        ah, al     
00b2 ebb8               jmp         0x6c       
```
Final part, deals with codes that are above 0x7FF, drop low 12 bits,
apply 0xE0 (see [UTF-8 encoding description](https://en.wikipedia.org/wiki/UTF-8#Description) for reference) and print it out,
adjust lower 12 bits and apply 8080 mask and again reuse part that spits out two chars.
[Answer]
# JavaScript, 64 bytes
```
x=>new TextDecoder('cp1252').decode(new TextEncoder().encode(x))
```
```
f =
x=>new TextDecoder('cp1252').decode(new TextEncoder().encode(x))
```
```
<p><input id=i oninput="o.value=f(i.value)" style="width:100%" /></p>
<p><output id=o></output></p>
```
Even longer than Java answer. So sad. :(
[Answer]
## [PHP](http://php.net/)+[mbstring](http://php.net/manual/en/book.mbstring.php), ~~63~~ 49 bytes
`<?=mb_convert_encoding($argv[1],'UTF8','CP1252');`
It doesn't work on TIO due to the lack of mbstring.
The third parameter force mbstring to interpret the string as Windows-1252 encoded
-14 bytes thanks to Ismael Miguel
[Answer]
# [C (gcc)](https://gcc.gnu.org/) + [`libiconv`](https://www.gnu.org/software/libiconv/), 119 117 bytes
```
*f(s,t,u)void*s,*t,*u;{long i=strlen(s),j=i*4;u=t=malloc(j);iconv(iconv_open("UTF8","CP1252"),&s,&i,&u,&j);return t;}
```
[Try it online!](https://tio.run/##XY1NasMwEIX3PsXggpHEpODQQkB1NoFAd1mkqxKCKzvOBEUK@vEm5CA9QC/Wi6iKV203j/e@ebxRs0Gp9EBG6dj18OJDR/bxuCx@I0dm@M86TR9/GSlrxoySODCPASMfLXXCowgoorxqawagJs/p3jDP8dSQeJKxCc251doqduJyGmGT7u0l98q37XpRYrna1PPnecmx8lgRVhGrXHd9iM5AkLdEJsC5JcPupnWDQnVsHQiR/cjhWgDQgd0Py5rDJQbPpji@1zvOZXFL6RW@P78gP5wtfgA "C (gcc) – Try It Online")
[Answer]
# Scala 3, 18 bytes
Simply a port of [Kevin Cruijssen's Java answer](https://codegolf.stackexchange.com/a/167239/95792)
```
String(_,"cp1252")
```
[Try it in Scastie!](https://scastie.scala-lang.org/gGDVsWJ4RICsWZt8LY1QuQ)
Ordinarily, I wouldn't post such a trivial answer, but this demonstrates Scala 3's universal apply methods, which let you omit `new`.
]