What's the proper way to initialize an unsigned integer to 0xFFFFFFFF in Powershell? - powershell

My CRC calculation algorithm requires that I initialize a 32-bit unsigned integer variable to 0xFFFFFFFF (all 1's in binary so that I can use the variable as a bitmask). If I just write [uint32]$r=0xFFFFFFFF, I get the following error message:
Cannot convert value "-1" to type "System.UInt32". Error: "Value was either too large or too small for a UInt32."
The syntax I'm currently using is [uint32]$r="0xFFFFFFFF", but it seems a bit over the top with the string to integer conversion (I'm coming from the C/C++ programming world). I'm pretty new to Powershell, so I was wondering if Powershell has a more efficient/straightforward way of initializing a variable like this.

How about [UInt32]::MaxValue.
The value of this constant is 4,294,967,295; that is, hexadecimal 0xFFFFFFFF.

PowerShell doesn't have any syntax for UInt32 literals, but you could cast an Int64 literal to [uint32] with the l type postfix:
[uint32]0xFFFFFFFFl

Related

sprintf expects argument of type char * - but type IS char

Thats code:
void bleAdvData(char *advData, uint8_t size){
char command[18+size];
uint8_t commandUint[18+size];
sprintf(command, "AT+BLEADVDATA=\"%s\"\r\n", *advData);
Warning in sprintf line:
Argument %s expects argument of type "char *", but argument 3 has type int
Why?
And what i have to do:
I want to trasfer a string(advData) wtich the length of "size" into a function for get a string command like:
AT+BLEADVDATA="advData"\r\n
Your variable advData is defined as char * in the argument list. This is a pointer to an address where character data is stored. However, in your sprintf() you use *advData, ie the actual place where advData points to, not the address itself.
Take the * off in the sprintf(), and all should be fine.
To clarify: char *advData on the first line makes advData a char *.
But then you added an asterisk to advData so you have * (char *advData).
So you want this:
sprintf(command, "AT+BLEADVDATA=\"%s\"\r\n", advData);
That extra asterisk "dereferences" the advData so you're now trying to pass in the first character of the string
sprintf then complains since that's not a valid string. If you ran this it'd either crash or on the ESP32 give you gibberish.
I recommend using the Warnings as Errors option on the ESP32. It's very rare that a warning won't be meaningful, and the ESP32 doesn't crash as easily as a program running on a modern PC OS.
That leads to really hard to find bugs where stuff randomly works or crashes with no clear pattern.

atobin() and atohex() in systemverilog

Does anyone know about these 2 functions? Should the output of 'F'.atohex() be 0x16 or 0x46 (directly from the ASCII table)? I have googled this already, but some said the former one is correct while some said the other. Thanks.
Actually, the result is 0xF. These functions do not have the greatest names. What both do is convert an ASCII string in a particular radix to an integral value. atohex assumes the string is formatted in hexadecimal.
from LRM:
— str.atoi() returns the integer corresponding to the ASCII decimal representation in str.
— atohex interprets the string as hexadecimal.
— atooct interprets the string as octal.
— atobin interprets the string as binary.
NOTE—These ASCII conversion functions return a 32-bit integer value
So, the result of the following:
string a = "F";
a.atohex();
ia a 32-bit integer: 32'hF.

How do you convert data types in Windows Powershell?

I'm very new to powershell and am running into walls trying to convert a string to a integer.
If I run the following command: Get-DefaultAudioDeviceVolume it often returns a number that looks something like: 50.05816%, which I have confirmed to be a string. I need to convert this to a whole number integer (50). Obviously I could hard code the integer in my script, but for the purpose of the script I need it to be flexible in it's conversion. The result of the previous test changes and I want to pass along the whole integer further down the line.
Thanks in advance!
If the string contains the % symbol you would need to remove this, then you can use the -as operator to convert to [int]
[string]$vol = "50.05816%"
$vol_int = $vol.Replace('%','') -as [int]
The -as operator is very useful and has many other uses, this article goes through a number of them: https://mcpmag.com/articles/2013/08/13/utilizing-the-as-operator.aspx
Just cast it to integer and replace the "%" with nothing:
[int]$var = (Get-DefaultAudioDeviceVolume).Replace("%","")
Powershell does automatic type casting and starts from the left. So when $var is defined as an integer, it will try to convert the right side to the same type.

How to truncate a 2's complement output

I have data written into short data type. The data written is of 2's complement form.
Now when I try to print the data using %04x, the data with MSB=0 is printed fine for eg if data=740, print I get is 0740
But when the MSB=1, I am unable to get a proper print. For eg if data=842, print I get is fffff842
I want the data truncated to 4 bytes so expected output is f842
Either declare your data as a type which is 16 bits long, or make sure the printing function uses the right format for 16 bits value. Or use your current type, but do a bitwise AND with 0xffff. What you can do depends on the language you're doing it in really.
But whichever way you go, check your assumptions again. There seems to be a few issues in your question:
2s-complement applies to signed numbers only. There are no negative numbers in your question.
Assuming you mean C's short - it doesn't have to be 16 bits long.
"I get is fffff842 I want the data truncated to 4 bytes" - fffff842 is 4 bytes long. f842 is 2 bytes long.
2-bytes long value 842 does not have the MSB set.
I'm assuming C (or possibly C++) as the language here.
Because of the default argument promotions involved when calling a variable argument function (such as printf), your use of a short will result in an integer promotion, which states that "If an int can represent all values of the original type (as restricted by the width, for a
bit-field), the value is converted to an int".
A short is converted to an int by means of sign-extension, and 0xf842 sign-extended to 32 bits is 0xfffff842.
You can use a bitwise AND to mask off the most significant word:
printf("%04x", data & 0xffff);
You could also add the h length specifier to state that you only want to print an (unsigned) short worth of bits from an int:
printf("%04hx", data);

Ada - Convert File_Size to Integer

I'm using Ada's Directories library and Command_Line library, and reading the character count (size) of a text file given in the command line, then assigning it to an integer.
Character_Count := Size(Argument(1));
The compiler is telling me that Integer and File_Size don't match up, even though File_Size is a subtype of Integer, I'm pretty sure. How can I convert it?
Ada.Directories.File_Size is not a subtype of Integer.
It's defined in the language reference manual as:
type File_Size is range 0 .. *implementation-defined*;
If you think about it, it wouldn't make much sense for it to be a subtype; Integer can be as narrow as 16 bits, which is hardly enough to hold the size of an arbitrary file.
You can use a conversion to convert to Integer:
Character_Count := Integer(Size(Argument(1)));
but it would probably be much better to declare Character_Count as a File_Size in the first place.