How to generate a 32 bit big-endian number in the format 0x00000001 in erlangCounting in Erlang (how do I increment a variable?)How do you do modulo or remainder in Erlang?Speed comparison with Project Euler: C vs Python vs Erlang vs HaskellErlang bit indexingErlang Bit Syntax: How does it knows that it's 3 components?How to generate a random alphanumeric string with Erlang?How can I handle Account Number in erlang?Erlang basic general server debugger output interpretationRead 16 bit little-endian, then parse as a bitstring in erlangBit Syntax and Binary Representation in ErlangWhy does Erlang generate the same sequence of random number if applying the same seed?
How to properly understand branches of complex functions
I just entered the USA without passport control at Atlanta airport
Improve appearance of the table in Latex
Is there any proof that high saturation and contrast makes a picture more appealing in social media?
Why isn't it a compile-time error to return a nullptr as a std::string?
Can I enter the UK for 24 hours from a Schengen area, holding an Indian passport?
What are the pros and cons for the two possible "gear directions" when parking the car on a hill?
Is the continuity test limit resistance of a multimeter standard?
Counterfeit checks were created for my account. How does this type of fraud work?
What happened to Hopper's girlfriend in season one?
Am I legally required to provide a (GPL licensed) source code even after a project is abandoned?
Umlaut character order when sorting
Rejecting an offer after accepting it just 10 days from date of joining
Encounter design and XP thresholds
In Mistborn, why can metal in people's bodies be affected by Allomancy sometimes?
What triggered jesuits' ban on infinitesimals in 1632?
Print one file per line using echo
Explain why a line can never intersect a plane in exactly two points.
Novel in which alien (Martian?) is trapped on Earth in prehistory
Is "Busen" just the area between the breasts?
"Correct me if I'm wrong"
How many people are necessary to maintain modern civilisation?
Is it illegal to withhold someone's passport and green card in California?
Is there a difference between an NFC and RFID chip?
How to generate a 32 bit big-endian number in the format 0x00000001 in erlang
Counting in Erlang (how do I increment a variable?)How do you do modulo or remainder in Erlang?Speed comparison with Project Euler: C vs Python vs Erlang vs HaskellErlang bit indexingErlang Bit Syntax: How does it knows that it's 3 components?How to generate a random alphanumeric string with Erlang?How can I handle Account Number in erlang?Erlang basic general server debugger output interpretationRead 16 bit little-endian, then parse as a bitstring in erlangBit Syntax and Binary Representation in ErlangWhy does Erlang generate the same sequence of random number if applying the same seed?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I need to generate a variable which has the following properties -
32 bit, big-endian integer, initialized with 0x00000001 (I'm going to increment that number one by one). Is there a syntax in erlang for this?
erlang
add a comment |
I need to generate a variable which has the following properties -
32 bit, big-endian integer, initialized with 0x00000001 (I'm going to increment that number one by one). Is there a syntax in erlang for this?
erlang
add a comment |
I need to generate a variable which has the following properties -
32 bit, big-endian integer, initialized with 0x00000001 (I'm going to increment that number one by one). Is there a syntax in erlang for this?
erlang
I need to generate a variable which has the following properties -
32 bit, big-endian integer, initialized with 0x00000001 (I'm going to increment that number one by one). Is there a syntax in erlang for this?
erlang
erlang
asked Mar 25 at 7:08
hasihasi
175
175
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
In Erlang, normally you'd keep such numbers as plain integers inside the program:
X = 1.
or equivalently, if you want to use a hexadecimal literal:
X = 16#00000001.
And when it's time to convert the number to a binary representation in order to send it somewhere else, use bit syntax:
<<X:32/big>>
This returns a binary containing four bytes:
<<0,0,0,1>>
(That's a 32-bit big-endian integer. In fact, big-endian is the default, so you could just write <<X:32>>
. <<X:64/little>>
would be a 64-bit little-endian integer.)
On the other hand, if you just want to print the number in 0x00000001
format, use io:format
with this format specifier:
io:format("0x~8.16.0b~n", [X]).
The 8
tells it to use a field width of 8 characters, the 16
tells it to use radix 16 (i.e. hexadecimal), and the 0
is the padding character, used for filling the number up to the field width.
Note that incrementing a variable works differently in Erlang compared to other languages. Once a variable has been assigned a value, you can't change it, so you'd end up making a recursive call, passing the new value as an argument to the function. This answer has an example.
add a comment |
According to the documentation[1] the following snippet should generate a 32-bit signed integer in little endian.
1> I = 258.
258
2> B = <<I:4/little-signed-integer-unit:8>>.
<<2,1,0,0>>
And the following should produce big endian numbers:
1> I = 258.
258
2> B = <<I:4/big-signed-integer-unit:8>>.
<<0,0,1,2>>
[1] http://erlang.org/doc/programming_examples/bit_syntax.html
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55332733%2fhow-to-generate-a-32-bit-big-endian-number-in-the-format-0x00000001-in-erlang%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In Erlang, normally you'd keep such numbers as plain integers inside the program:
X = 1.
or equivalently, if you want to use a hexadecimal literal:
X = 16#00000001.
And when it's time to convert the number to a binary representation in order to send it somewhere else, use bit syntax:
<<X:32/big>>
This returns a binary containing four bytes:
<<0,0,0,1>>
(That's a 32-bit big-endian integer. In fact, big-endian is the default, so you could just write <<X:32>>
. <<X:64/little>>
would be a 64-bit little-endian integer.)
On the other hand, if you just want to print the number in 0x00000001
format, use io:format
with this format specifier:
io:format("0x~8.16.0b~n", [X]).
The 8
tells it to use a field width of 8 characters, the 16
tells it to use radix 16 (i.e. hexadecimal), and the 0
is the padding character, used for filling the number up to the field width.
Note that incrementing a variable works differently in Erlang compared to other languages. Once a variable has been assigned a value, you can't change it, so you'd end up making a recursive call, passing the new value as an argument to the function. This answer has an example.
add a comment |
In Erlang, normally you'd keep such numbers as plain integers inside the program:
X = 1.
or equivalently, if you want to use a hexadecimal literal:
X = 16#00000001.
And when it's time to convert the number to a binary representation in order to send it somewhere else, use bit syntax:
<<X:32/big>>
This returns a binary containing four bytes:
<<0,0,0,1>>
(That's a 32-bit big-endian integer. In fact, big-endian is the default, so you could just write <<X:32>>
. <<X:64/little>>
would be a 64-bit little-endian integer.)
On the other hand, if you just want to print the number in 0x00000001
format, use io:format
with this format specifier:
io:format("0x~8.16.0b~n", [X]).
The 8
tells it to use a field width of 8 characters, the 16
tells it to use radix 16 (i.e. hexadecimal), and the 0
is the padding character, used for filling the number up to the field width.
Note that incrementing a variable works differently in Erlang compared to other languages. Once a variable has been assigned a value, you can't change it, so you'd end up making a recursive call, passing the new value as an argument to the function. This answer has an example.
add a comment |
In Erlang, normally you'd keep such numbers as plain integers inside the program:
X = 1.
or equivalently, if you want to use a hexadecimal literal:
X = 16#00000001.
And when it's time to convert the number to a binary representation in order to send it somewhere else, use bit syntax:
<<X:32/big>>
This returns a binary containing four bytes:
<<0,0,0,1>>
(That's a 32-bit big-endian integer. In fact, big-endian is the default, so you could just write <<X:32>>
. <<X:64/little>>
would be a 64-bit little-endian integer.)
On the other hand, if you just want to print the number in 0x00000001
format, use io:format
with this format specifier:
io:format("0x~8.16.0b~n", [X]).
The 8
tells it to use a field width of 8 characters, the 16
tells it to use radix 16 (i.e. hexadecimal), and the 0
is the padding character, used for filling the number up to the field width.
Note that incrementing a variable works differently in Erlang compared to other languages. Once a variable has been assigned a value, you can't change it, so you'd end up making a recursive call, passing the new value as an argument to the function. This answer has an example.
In Erlang, normally you'd keep such numbers as plain integers inside the program:
X = 1.
or equivalently, if you want to use a hexadecimal literal:
X = 16#00000001.
And when it's time to convert the number to a binary representation in order to send it somewhere else, use bit syntax:
<<X:32/big>>
This returns a binary containing four bytes:
<<0,0,0,1>>
(That's a 32-bit big-endian integer. In fact, big-endian is the default, so you could just write <<X:32>>
. <<X:64/little>>
would be a 64-bit little-endian integer.)
On the other hand, if you just want to print the number in 0x00000001
format, use io:format
with this format specifier:
io:format("0x~8.16.0b~n", [X]).
The 8
tells it to use a field width of 8 characters, the 16
tells it to use radix 16 (i.e. hexadecimal), and the 0
is the padding character, used for filling the number up to the field width.
Note that incrementing a variable works differently in Erlang compared to other languages. Once a variable has been assigned a value, you can't change it, so you'd end up making a recursive call, passing the new value as an argument to the function. This answer has an example.
edited Mar 25 at 9:54
answered Mar 25 at 9:36
legoscialegoscia
30.3k1187119
30.3k1187119
add a comment |
add a comment |
According to the documentation[1] the following snippet should generate a 32-bit signed integer in little endian.
1> I = 258.
258
2> B = <<I:4/little-signed-integer-unit:8>>.
<<2,1,0,0>>
And the following should produce big endian numbers:
1> I = 258.
258
2> B = <<I:4/big-signed-integer-unit:8>>.
<<0,0,1,2>>
[1] http://erlang.org/doc/programming_examples/bit_syntax.html
add a comment |
According to the documentation[1] the following snippet should generate a 32-bit signed integer in little endian.
1> I = 258.
258
2> B = <<I:4/little-signed-integer-unit:8>>.
<<2,1,0,0>>
And the following should produce big endian numbers:
1> I = 258.
258
2> B = <<I:4/big-signed-integer-unit:8>>.
<<0,0,1,2>>
[1] http://erlang.org/doc/programming_examples/bit_syntax.html
add a comment |
According to the documentation[1] the following snippet should generate a 32-bit signed integer in little endian.
1> I = 258.
258
2> B = <<I:4/little-signed-integer-unit:8>>.
<<2,1,0,0>>
And the following should produce big endian numbers:
1> I = 258.
258
2> B = <<I:4/big-signed-integer-unit:8>>.
<<0,0,1,2>>
[1] http://erlang.org/doc/programming_examples/bit_syntax.html
According to the documentation[1] the following snippet should generate a 32-bit signed integer in little endian.
1> I = 258.
258
2> B = <<I:4/little-signed-integer-unit:8>>.
<<2,1,0,0>>
And the following should produce big endian numbers:
1> I = 258.
258
2> B = <<I:4/big-signed-integer-unit:8>>.
<<0,0,1,2>>
[1] http://erlang.org/doc/programming_examples/bit_syntax.html
answered Mar 25 at 9:33
Christophe De TroyerChristophe De Troyer
1,48722139
1,48722139
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55332733%2fhow-to-generate-a-32-bit-big-endian-number-in-the-format-0x00000001-in-erlang%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown