Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encode a string in UTF-8

I want to encode a string into UTF8 in PowerShell.

This is what I tried:

$consumer_key ="xvz1evFS4wEEPTGEFPHBog"
$enc_consumer_key = System.Text.UTF8Encoding($consumer_key)

But I get an error:

System.Text.UTF8Encoding in not recognize as the name of cmdlet

like image 965
user3562182 Avatar asked Apr 23 '14 23:04

user3562182


People also ask

How do I encode a string in UTF-8?

In order to convert a String into UTF-8, we use the getBytes() method in Java. The getBytes() method encodes a String into a sequence of bytes and returns a byte array. where charsetName is the specific charset by which the String is encoded into an array of bytes.

What is encoding =' UTF-8?

UTF-8 is an encoding system for Unicode. It can translate any Unicode character to a matching unique binary string, and can also translate the binary string back to a Unicode character. This is the meaning of “UTF”, or “Unicode Transformation Format.”

What does encoding =' UTF-8 do in Python?

UTF-8 is a byte oriented encoding. The encoding specifies that each character is represented by a specific sequence of one or more bytes.

Can UTF-8 encode all characters?

UTF-8 is capable of encoding all 1,112,064 valid character code points in Unicode using one to four one-byte (8-bit) code units. Code points with lower numerical values, which tend to occur more frequently, are encoded using fewer bytes.


2 Answers

Try this instead:

$enc = [System.Text.Encoding]::UTF8
$consumerkey ="xvz1evFS4wEEPTGEFPHBog"
$encconsumerkey= $enc.GetBytes($consumerkey)
like image 104
justpaul Avatar answered Oct 05 '22 09:10

justpaul


If you just want to write the string to file:

$consumer_key ="xvz1evFS4wEEPTGEFPHBog"
$consumer_key |  Out-File c:\path\utf8file.txt -Encoding UTF8
like image 43
Raf Avatar answered Oct 05 '22 10:10

Raf