Decoding 32bit unsigned int in 4 unsigned char in Perl

Andrea Mocci

I have to write in a binary file these hex values:

my $erase_start = 0x00010000;
my $erase_size  = 0x00008000;

And this is the expected result

enter image description here

Using

print WRITEHANDLE sprintf('%08X', $erase_start);
print WRITEHANDLE sprintf('%08X', $erase_size);

I obtain

enter image description here

I was taking a look to pack/unpack capabilities, but I am not a Perl Guru.

Kjetil S.

You want to write those 32-bit unsigned ints to a binary file. So I guess you want something like this:

open my $fh, ">", "file.dta" or die;
binmode($fh);
print $fh pack("N*", 0x00010000, 0x00008000);   #one or more u32 values
close($fh);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert from char array to 16bit signed int and 32bit unsigned int

How to convert 16 bit unsigned int to 8 bit unsigned char & finally return in an unsigned char*?

Casting Int as Unsigned Char

How to convert unsigned char * to unsigned int * in c?

memcpy from unsigned char * to unsigned int

Converting unsigned char to int and short

Casting from `int` to `unsigned char`

Converting unsigned char array to int

Arduino unsigned long int to char*

C++: Unsigned Char to unsigned int using pointers without bit shifts

In case of bit fields, which one is better to use, unsigned char or unsigned int and why?

IA32 Assembly code for type casting from signed/unsigned char to unsigned/signed int

Converting 4 bytes to unsigned int

small bitoperation problem with unsigned int in combination with unsigned char

Overloading function in C++ as unsigned char and unsigned int result in ambiguous

How to convert unsigned char to unsigned int in c++?

How can I convert unsigned int to unsigned char

How to convert unsigned long long int into unsigned char*?

Why is std::size_t 4 bytes on 32bit systems when unsigned long long is 8 bytes on both 32bit and 64 bit systems?

Setting nth bit of unsigned int

Searching for a bit pattern in an unsigned int

Warning on char or int or short or long or unsigned

Is resolving char between signed and unsigned int unspecified?

Objective C unsigned char to negative int

objective c convert int to unsigned char hex

Consequences of uninitialised variables: int vs unsigned char

Convert int to unsigned char array and backwards

Unsigned char * added with int? What does this do?

Narrowing conversion from int to unsigned char