I am expecting the character uppercase E from the following (I was using some C code as an example that works)
$Char = [char]([convert]::toint16('BA',16))
[char]($Char -bor 0xff)
RETURNS ÿ
What am I doing wrong?
for (UINT i = 0; i < in.length(); i++)
{
unsigned char bit = in;
bit ^= 0xff;
out.append((char*)&bit, 1);
}
bitwise problem
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Re: bitwise problem
Forget about C code. This is PowerShell and Net Framework which has advanced character manipulation routines.
What is it you are really trying to accomplish in simple language?
What is it you are really trying to accomplish in simple language?
Re: bitwise problem
Also note that 'BA' hex is NOT the character 'E'. The hex for 'E' is 0x45.
[convert]::ToChar(0x45)
The simplest way to convert a hex byte into a character is the following:
[char]0x45
[convert]::ToChar(0x45)
The simplest way to convert a hex byte into a character is the following:
[char]0x45
Re: bitwise problem
So the full story is that we have a hash string that we are intending to read to embed an expiry into our code. We have C exe's that are using the same code and I am attempting to replicate it into our powershell products.
There is much more to the hash string than the 'BA' but I was just using the first 'BA' as an example. Below is the C code and I was looking to replicate with PS
std::string hexToASCII(std::string hex)
{
// initialize the ASCII code string as empty.
std::string ascii = "";
for (size_t i = 0; i < hex.length(); i += 2)
{
// extract two characters from hex string
std::string part = hex.substr(i, 2);
// change it into base 16 and
// typecast as the character
char ch = (char)stoul(part, nullptr, 16);
// add this char to final ASCII string
ascii += ch;
}
return ascii;
}
void StringBitFlip(std::string in, std::string& out)
{
out.erase();
for (UINT i = 0; i < in.length(); i++)
{
unsigned char bit = in;
bit ^= 0xff;
out.append((char*)&bit, 1);
}
};
I thought the equivalent PS would look something like..
$SpecHash = 'BA'
$Ascii=''
($SpecHash -split '(.{2})') | where-object {$_} | foreach-object{
$Char = [char]([convert]::toint16($_,16))
$Ascii += $Char
}
($Ascii -split '(.{1})') | where-object {$_} | foreach-object{
$char = $_ -bor 0xff
}
# expecting $char to equal 'E'
There is much more to the hash string than the 'BA' but I was just using the first 'BA' as an example. Below is the C code and I was looking to replicate with PS
std::string hexToASCII(std::string hex)
{
// initialize the ASCII code string as empty.
std::string ascii = "";
for (size_t i = 0; i < hex.length(); i += 2)
{
// extract two characters from hex string
std::string part = hex.substr(i, 2);
// change it into base 16 and
// typecast as the character
char ch = (char)stoul(part, nullptr, 16);
// add this char to final ASCII string
ascii += ch;
}
return ascii;
}
void StringBitFlip(std::string in, std::string& out)
{
out.erase();
for (UINT i = 0; i < in.length(); i++)
{
unsigned char bit = in;
bit ^= 0xff;
out.append((char*)&bit, 1);
}
};
I thought the equivalent PS would look something like..
$SpecHash = 'BA'
$Ascii=''
($SpecHash -split '(.{2})') | where-object {$_} | foreach-object{
$Char = [char]([convert]::toint16($_,16))
$Ascii += $Char
}
($Ascii -split '(.{1})') | where-object {$_} | foreach-object{
$char = $_ -bor 0xff
}
# expecting $char to equal 'E'
Re: bitwise problem
So you are trying to "hash" a string. Windows and the Net Framework has complete support for hashing in various ways built into the "Cryptography" classes.
If you are asking for someone to convert old 'C' code to PowerShell, then I recommend hiring a consultant or finding a programmer in you company that knows the Net Framework and C.
I answered the question you asked but now it turns out that that wasn't the real question.
Note that ther eaare many different types of "hashes" and it loks like you are just doing a simple bit scramble and not a hash.
Here is an article that demonstrates how to do an MD5 hash in PS.
https://community.idera.com/database-to ... -from-text
Here are articles on various ways to hash with PowerPowerShell.
https://www.google.com/search?q=powersh ... nt=gws-wiz
If you are asking for someone to convert old 'C' code to PowerShell, then I recommend hiring a consultant or finding a programmer in you company that knows the Net Framework and C.
I answered the question you asked but now it turns out that that wasn't the real question.
Note that ther eaare many different types of "hashes" and it loks like you are just doing a simple bit scramble and not a hash.
Here is an article that demonstrates how to do an MD5 hash in PS.
https://community.idera.com/database-to ... -from-text
Here are articles on various ways to hash with PowerPowerShell.
https://www.google.com/search?q=powersh ... nt=gws-wiz