2

I have read Microsoft powershell official documentation and i've found this example:

"`u{0048}"

It should display an H

But, i my case, it displays:

u{0048}

I do not need alternatives, i want to understand why it does not work.

Thanks

2 Answers 2

3

The `u{XXXX} escape sequence was added to the string expansion grammar in PowerShell 6, and will not be recognized as such in any version of Windows PowerShell.


In Windows PowerShell you can cast the numerical value to [char] in a sub-expression:

"$([char]0x48)"

Or use the -as operator to convert to [char]:

0x48 -as [char]
# or
0x48 -as 'char'

If you're trying to produce the [char] value H without using h, H, [ or ] in the source code, there are plenty of opportunities in PowerShell, ForEach-Object's aliases and member-invocation-via-wildcards is especially handy:

# grab the `h` in `$env:path`
(dir env:pat?).Name.GetEnumerator()|select -Last 1

# reflect against another existing [char] to get the type
$g = 'g'|% ToC?ar ($null)
0x48 -as $g.GetType()

# generate char range between G and I, filter out G and I (pwsh >6.1 only)
'G'..'I'|?{$_-notin'G','I'}
Sign up to request clarification or add additional context in comments.

7 Comments

I am working on a CTF challenge and i am not allowed to type [ char. Is there a way to do this with out brackets ? Thanks..
@Bob5421 Do an unchecked cast with the target type name as a string: 0x48 -as 'char'
h char is forbidden too :)
Well, then you'll have to figure out a way to obtain some reference to a [char] type or the string char :)
@Bob5421 I'm not gonna update this answer any further until you update your question with ALL the relevant details
|
0

The backtick character before a variable tells PowerShell not to resolve the variable but print its name. That's why it displayed what it did.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.