Little Endian num 2 string conversion 🔃Long multiply, 8 bits at a timeGraduation Meme TallyBig or Little...
How can I adjust the sequential numbering scheme when exporting Photos?
Would 5.5 x 2.1 mm male plug work inside a 5.5 x 2.5 mm female jack for DC power?
What can I do to avoid potential charges for bribery?
Why do English transliterations of Arabic names have so many Qs in them?
What does IKEA-like mean?
Test if two food are the same
Little Endian num 2 string conversion 🔃
Meaning/translation of title "The Light Fantastic" By Terry Pratchett
one-liner vs script
QGIS can't detect negative values?
How acceptable is an ellipsis "..." in formal mathematics?
recreating old mechanisme
Why didn't Kes send Voyager home?
"Es gefällt ihm." How to identify similar exceptions?
Can massive damage kill you while at 0 HP?
How can we check whether the user input equal to one elements of an array?
Why does transition from one electron shell to another shell always produce massless photon?
Are there any official mechanics for grafts or anything similar?
Can I perform Umrah while on a Saudi Arabian visit e-visa
How to discipline overeager engineer
How to make a gift without seeming creepy?
I am confused with the word order when putting a sentence into passé composé with reflexive verbs
How can I curtail abuse of the Illusion wizard's Illusory Reality feature?
An employee has low self-confidence, and is performing poorly. How can I help?
Little Endian num 2 string conversion 🔃
Long multiply, 8 bits at a timeGraduation Meme TallyBig or Little EndianNumber Plate Golf: ParsingWhere can the cannon go?Eyeballing the bitsSwap the EndiannessExecute Triangularity MoveIncrement a GUIDSuper Mario in Colors :)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}
$begingroup$
Introduction
During work with BMP (bitmap) generator I face problem of converting number to little endian hex string. Here is function which I create in JavaScript - but wonder how small code can works similarly
let liEnd= num => num.toString(16).padStart(8,'0').match(/../g).reverse().join``;
console.log(liEnd(304767)) // 304767 dec = 0x4a67f hex
Challenge
Write function which will take 32bit decimal unsigned integer number on input, and produce 8-digit hexadecimal string with little endian order. The example algorithm which do the job:
- convert numb to hex string e.g:
304767 -> '4a67f'
- add padding zeros to get 8-char string:
'0004a67f'
- split string to four 2-char pieces:
'00','04','a6','7f'
- reverse order of pieces
'7f','a6','04','00'
- join pieces and return as result:
'7fa60400'
Example Input and Output
Input number (or string with dec number) is on the left of ->
, output hex string is on the right
2141586432 -> 0004a67f
304767 -> 7fa60400
code-golf
$endgroup$
add a comment
|
$begingroup$
Introduction
During work with BMP (bitmap) generator I face problem of converting number to little endian hex string. Here is function which I create in JavaScript - but wonder how small code can works similarly
let liEnd= num => num.toString(16).padStart(8,'0').match(/../g).reverse().join``;
console.log(liEnd(304767)) // 304767 dec = 0x4a67f hex
Challenge
Write function which will take 32bit decimal unsigned integer number on input, and produce 8-digit hexadecimal string with little endian order. The example algorithm which do the job:
- convert numb to hex string e.g:
304767 -> '4a67f'
- add padding zeros to get 8-char string:
'0004a67f'
- split string to four 2-char pieces:
'00','04','a6','7f'
- reverse order of pieces
'7f','a6','04','00'
- join pieces and return as result:
'7fa60400'
Example Input and Output
Input number (or string with dec number) is on the left of ->
, output hex string is on the right
2141586432 -> 0004a67f
304767 -> 7fa60400
code-golf
$endgroup$
$begingroup$
Does outputting to stdout count or does it have to be a string?
$endgroup$
– JL2210
6 hours ago
$begingroup$
@JL2210 if your technology not support strings, then yes, you can return result in any human readable way
$endgroup$
– Kamil Kiełczewski
6 hours ago
$begingroup$
Again, do you really need the emoji in the title?
$endgroup$
– Jo King
1 hour ago
add a comment
|
$begingroup$
Introduction
During work with BMP (bitmap) generator I face problem of converting number to little endian hex string. Here is function which I create in JavaScript - but wonder how small code can works similarly
let liEnd= num => num.toString(16).padStart(8,'0').match(/../g).reverse().join``;
console.log(liEnd(304767)) // 304767 dec = 0x4a67f hex
Challenge
Write function which will take 32bit decimal unsigned integer number on input, and produce 8-digit hexadecimal string with little endian order. The example algorithm which do the job:
- convert numb to hex string e.g:
304767 -> '4a67f'
- add padding zeros to get 8-char string:
'0004a67f'
- split string to four 2-char pieces:
'00','04','a6','7f'
- reverse order of pieces
'7f','a6','04','00'
- join pieces and return as result:
'7fa60400'
Example Input and Output
Input number (or string with dec number) is on the left of ->
, output hex string is on the right
2141586432 -> 0004a67f
304767 -> 7fa60400
code-golf
$endgroup$
Introduction
During work with BMP (bitmap) generator I face problem of converting number to little endian hex string. Here is function which I create in JavaScript - but wonder how small code can works similarly
let liEnd= num => num.toString(16).padStart(8,'0').match(/../g).reverse().join``;
console.log(liEnd(304767)) // 304767 dec = 0x4a67f hex
Challenge
Write function which will take 32bit decimal unsigned integer number on input, and produce 8-digit hexadecimal string with little endian order. The example algorithm which do the job:
- convert numb to hex string e.g:
304767 -> '4a67f'
- add padding zeros to get 8-char string:
'0004a67f'
- split string to four 2-char pieces:
'00','04','a6','7f'
- reverse order of pieces
'7f','a6','04','00'
- join pieces and return as result:
'7fa60400'
Example Input and Output
Input number (or string with dec number) is on the left of ->
, output hex string is on the right
2141586432 -> 0004a67f
304767 -> 7fa60400
let liEnd= num => num.toString(16).padStart(8,'0').match(/../g).reverse().join``;
console.log(liEnd(304767)) // 304767 dec = 0x4a67f hex
let liEnd= num => num.toString(16).padStart(8,'0').match(/../g).reverse().join``;
console.log(liEnd(304767)) // 304767 dec = 0x4a67f hex
code-golf
code-golf
edited 8 hours ago
Kamil Kiełczewski
asked 8 hours ago
Kamil KiełczewskiKamil Kiełczewski
2429 bronze badges
2429 bronze badges
$begingroup$
Does outputting to stdout count or does it have to be a string?
$endgroup$
– JL2210
6 hours ago
$begingroup$
@JL2210 if your technology not support strings, then yes, you can return result in any human readable way
$endgroup$
– Kamil Kiełczewski
6 hours ago
$begingroup$
Again, do you really need the emoji in the title?
$endgroup$
– Jo King
1 hour ago
add a comment
|
$begingroup$
Does outputting to stdout count or does it have to be a string?
$endgroup$
– JL2210
6 hours ago
$begingroup$
@JL2210 if your technology not support strings, then yes, you can return result in any human readable way
$endgroup$
– Kamil Kiełczewski
6 hours ago
$begingroup$
Again, do you really need the emoji in the title?
$endgroup$
– Jo King
1 hour ago
$begingroup$
Does outputting to stdout count or does it have to be a string?
$endgroup$
– JL2210
6 hours ago
$begingroup$
Does outputting to stdout count or does it have to be a string?
$endgroup$
– JL2210
6 hours ago
$begingroup$
@JL2210 if your technology not support strings, then yes, you can return result in any human readable way
$endgroup$
– Kamil Kiełczewski
6 hours ago
$begingroup$
@JL2210 if your technology not support strings, then yes, you can return result in any human readable way
$endgroup$
– Kamil Kiełczewski
6 hours ago
$begingroup$
Again, do you really need the emoji in the title?
$endgroup$
– Jo King
1 hour ago
$begingroup$
Again, do you really need the emoji in the title?
$endgroup$
– Jo King
1 hour ago
add a comment
|
12 Answers
12
active
oldest
votes
$begingroup$
Python 3, 37 bytes
lambda n:n.to_bytes(4,"little").hex()
Try it online!
Arithmetic-based recursive solution (50 49 bytes, works also for Python 2):
f=lambda n,i=4:i*'1'and"%02x"%(n%256)+f(n>>8,i-1)
Try it online!
-1 byte thanks to @JonathanAllan
$endgroup$
$begingroup$
I'd say submit the recursive one as a Python 2 entry :)
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
f=lambda n,i=4:i*'1'and'%02x'%(n%256)+f(n>>8,i-1)
saves a byte :)
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
@JonathanAllan Thanks. I am not familiar with all Python 2 tricks and do not see how it can be made shorter though.
$endgroup$
– Joel
6 hours ago
$begingroup$
it doesn't but the 37 won't work in py 2
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
Yeah. Some of those built-ins are Python-3-only.
$endgroup$
– Joel
6 hours ago
|
show 2 more comments
$begingroup$
R, 54 53 bytes
format.hexmode(scan()%/%256^(0:3)%%256%*%256^(3:0),8)
Try it online!
Each group of 2 characters is actually the hex representation of a digit in base 256. scan()%/%256^(0:3)%%256
converts to a base 256 number with 4 digits reversed,
...%*%256^(3:0)
joins them as a single integer, and format.hexmode(...,8)
converts that number to its hex representation with 8 digits.
$endgroup$
add a comment
|
$begingroup$
05AB1E, 10 bytes
h8jð0:2ôRJ
Try it online!
h convert input to hex
8j extend to length 8
ð0: replace spaces by zeros
2ô split in groups of two chars
R reverse groups
J and join all
$endgroup$
add a comment
|
$begingroup$
JavaScript (ES7), 59 57 bytes
String manipulation.
n=>(n+2**32).toString(16).match(/B../g).reverse().join``
Try it online!
How?
We first convert $n + 2^{32}$ to hexadecimal to make sure that all leading $0$'s are included:
(304767 + 2**32).toString(16) // --> '10004a67f'
Try it online!
We use the regular expression /B../g
to match all groups of 2 digits, ignoring the leading $1$ thanks to B
(non-word boundary).
'10004a67f'.match(/B../g) // --> [ '00', '04', 'a6', '7f' ]
Try it online!
We reverse()
and join()
to get the final string.
JavaScript (ES6), 61 bytes
Recursive function.
f=(n,k=4)=>k?[(x=n&255)>>4&&'']+x.toString(16)+f(n>>8,k-1):''
Try it online!
$endgroup$
add a comment
|
$begingroup$
Japt -P
, 10 bytes
sG ùT8 ò w
Try it
$endgroup$
$begingroup$
What does-P
do?
$endgroup$
– JL2210
4 hours ago
add a comment
|
$begingroup$
Forth (gforth), 52 51 40 bytes
: f hex 0 4. do <# # # 0. #> type loop ;
Try it online!
Code explanation
: f start a new word definition
hex set the current base to base 16
0 convert the input number to a double-cell integer
4. do start a counted loop from 0 to 3
<# # # start a formatted numeric string and move last 2 digits to format area
0. move remaining digits down the stack
#> delete top two stack value and convert format area to string
type output string
loop end loop
; end word definition
$endgroup$
add a comment
|
$begingroup$
C# (Visual C# Interactive Compiler), 58 bytes
x=>$"{((x=x>>16|x<<16)&4278255360)>>8|(x&16711935)<<8:x8}"
Try it online!
$endgroup$
add a comment
|
$begingroup$
Jelly, 13 bytes
+Ø%b⁴Ḋs2Ṛ‘ịØh
Try it online!
A full program that takes an integer as its argument and prints a string.
$endgroup$
add a comment
|
$begingroup$
APL+WIN, 36 34 bytes
2 bytes saved by converting to index zero
Prompts for integer:
'0123456789abcdef'[,⊖4 2⍴(8⍴16)⊤⎕]
Try it online! Courtesy Dyalog Classic
$endgroup$
add a comment
|
$begingroup$
C (gcc), 30 bytes
f(x){printf("%.8x",htonl(x));}
Try it online!
$endgroup$
$begingroup$
@ceilingcat That wouldn't be portable to big-endian platforms. I'll take it, though. Thanks!
$endgroup$
– JL2210
5 hours ago
add a comment
|
$begingroup$
J, 10 bytes
8{._1{3!:3
Try it online!
$endgroup$
add a comment
|
$begingroup$
Zsh, 46 bytes
i=$1
repeat 4 printf %02x $[j=i%256,i=i/256,j]
Try it online!
$endgroup$
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: "200"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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/4.0/"u003ecc by-sa 4.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%2fcodegolf.stackexchange.com%2fquestions%2f193793%2flittle-endian-num-2-string-conversion%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
12 Answers
12
active
oldest
votes
12 Answers
12
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Python 3, 37 bytes
lambda n:n.to_bytes(4,"little").hex()
Try it online!
Arithmetic-based recursive solution (50 49 bytes, works also for Python 2):
f=lambda n,i=4:i*'1'and"%02x"%(n%256)+f(n>>8,i-1)
Try it online!
-1 byte thanks to @JonathanAllan
$endgroup$
$begingroup$
I'd say submit the recursive one as a Python 2 entry :)
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
f=lambda n,i=4:i*'1'and'%02x'%(n%256)+f(n>>8,i-1)
saves a byte :)
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
@JonathanAllan Thanks. I am not familiar with all Python 2 tricks and do not see how it can be made shorter though.
$endgroup$
– Joel
6 hours ago
$begingroup$
it doesn't but the 37 won't work in py 2
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
Yeah. Some of those built-ins are Python-3-only.
$endgroup$
– Joel
6 hours ago
|
show 2 more comments
$begingroup$
Python 3, 37 bytes
lambda n:n.to_bytes(4,"little").hex()
Try it online!
Arithmetic-based recursive solution (50 49 bytes, works also for Python 2):
f=lambda n,i=4:i*'1'and"%02x"%(n%256)+f(n>>8,i-1)
Try it online!
-1 byte thanks to @JonathanAllan
$endgroup$
$begingroup$
I'd say submit the recursive one as a Python 2 entry :)
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
f=lambda n,i=4:i*'1'and'%02x'%(n%256)+f(n>>8,i-1)
saves a byte :)
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
@JonathanAllan Thanks. I am not familiar with all Python 2 tricks and do not see how it can be made shorter though.
$endgroup$
– Joel
6 hours ago
$begingroup$
it doesn't but the 37 won't work in py 2
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
Yeah. Some of those built-ins are Python-3-only.
$endgroup$
– Joel
6 hours ago
|
show 2 more comments
$begingroup$
Python 3, 37 bytes
lambda n:n.to_bytes(4,"little").hex()
Try it online!
Arithmetic-based recursive solution (50 49 bytes, works also for Python 2):
f=lambda n,i=4:i*'1'and"%02x"%(n%256)+f(n>>8,i-1)
Try it online!
-1 byte thanks to @JonathanAllan
$endgroup$
Python 3, 37 bytes
lambda n:n.to_bytes(4,"little").hex()
Try it online!
Arithmetic-based recursive solution (50 49 bytes, works also for Python 2):
f=lambda n,i=4:i*'1'and"%02x"%(n%256)+f(n>>8,i-1)
Try it online!
-1 byte thanks to @JonathanAllan
edited 6 hours ago
answered 6 hours ago
JoelJoel
1,6111 silver badge12 bronze badges
1,6111 silver badge12 bronze badges
$begingroup$
I'd say submit the recursive one as a Python 2 entry :)
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
f=lambda n,i=4:i*'1'and'%02x'%(n%256)+f(n>>8,i-1)
saves a byte :)
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
@JonathanAllan Thanks. I am not familiar with all Python 2 tricks and do not see how it can be made shorter though.
$endgroup$
– Joel
6 hours ago
$begingroup$
it doesn't but the 37 won't work in py 2
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
Yeah. Some of those built-ins are Python-3-only.
$endgroup$
– Joel
6 hours ago
|
show 2 more comments
$begingroup$
I'd say submit the recursive one as a Python 2 entry :)
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
f=lambda n,i=4:i*'1'and'%02x'%(n%256)+f(n>>8,i-1)
saves a byte :)
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
@JonathanAllan Thanks. I am not familiar with all Python 2 tricks and do not see how it can be made shorter though.
$endgroup$
– Joel
6 hours ago
$begingroup$
it doesn't but the 37 won't work in py 2
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
Yeah. Some of those built-ins are Python-3-only.
$endgroup$
– Joel
6 hours ago
$begingroup$
I'd say submit the recursive one as a Python 2 entry :)
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
I'd say submit the recursive one as a Python 2 entry :)
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
f=lambda n,i=4:i*'1'and'%02x'%(n%256)+f(n>>8,i-1)
saves a byte :)$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
f=lambda n,i=4:i*'1'and'%02x'%(n%256)+f(n>>8,i-1)
saves a byte :)$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
@JonathanAllan Thanks. I am not familiar with all Python 2 tricks and do not see how it can be made shorter though.
$endgroup$
– Joel
6 hours ago
$begingroup$
@JonathanAllan Thanks. I am not familiar with all Python 2 tricks and do not see how it can be made shorter though.
$endgroup$
– Joel
6 hours ago
$begingroup$
it doesn't but the 37 won't work in py 2
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
it doesn't but the 37 won't work in py 2
$endgroup$
– Jonathan Allan
6 hours ago
$begingroup$
Yeah. Some of those built-ins are Python-3-only.
$endgroup$
– Joel
6 hours ago
$begingroup$
Yeah. Some of those built-ins are Python-3-only.
$endgroup$
– Joel
6 hours ago
|
show 2 more comments
$begingroup$
R, 54 53 bytes
format.hexmode(scan()%/%256^(0:3)%%256%*%256^(3:0),8)
Try it online!
Each group of 2 characters is actually the hex representation of a digit in base 256. scan()%/%256^(0:3)%%256
converts to a base 256 number with 4 digits reversed,
...%*%256^(3:0)
joins them as a single integer, and format.hexmode(...,8)
converts that number to its hex representation with 8 digits.
$endgroup$
add a comment
|
$begingroup$
R, 54 53 bytes
format.hexmode(scan()%/%256^(0:3)%%256%*%256^(3:0),8)
Try it online!
Each group of 2 characters is actually the hex representation of a digit in base 256. scan()%/%256^(0:3)%%256
converts to a base 256 number with 4 digits reversed,
...%*%256^(3:0)
joins them as a single integer, and format.hexmode(...,8)
converts that number to its hex representation with 8 digits.
$endgroup$
add a comment
|
$begingroup$
R, 54 53 bytes
format.hexmode(scan()%/%256^(0:3)%%256%*%256^(3:0),8)
Try it online!
Each group of 2 characters is actually the hex representation of a digit in base 256. scan()%/%256^(0:3)%%256
converts to a base 256 number with 4 digits reversed,
...%*%256^(3:0)
joins them as a single integer, and format.hexmode(...,8)
converts that number to its hex representation with 8 digits.
$endgroup$
R, 54 53 bytes
format.hexmode(scan()%/%256^(0:3)%%256%*%256^(3:0),8)
Try it online!
Each group of 2 characters is actually the hex representation of a digit in base 256. scan()%/%256^(0:3)%%256
converts to a base 256 number with 4 digits reversed,
...%*%256^(3:0)
joins them as a single integer, and format.hexmode(...,8)
converts that number to its hex representation with 8 digits.
edited 4 hours ago
answered 7 hours ago
Robin RyderRobin Ryder
4,2924 silver badges29 bronze badges
4,2924 silver badges29 bronze badges
add a comment
|
add a comment
|
$begingroup$
05AB1E, 10 bytes
h8jð0:2ôRJ
Try it online!
h convert input to hex
8j extend to length 8
ð0: replace spaces by zeros
2ô split in groups of two chars
R reverse groups
J and join all
$endgroup$
add a comment
|
$begingroup$
05AB1E, 10 bytes
h8jð0:2ôRJ
Try it online!
h convert input to hex
8j extend to length 8
ð0: replace spaces by zeros
2ô split in groups of two chars
R reverse groups
J and join all
$endgroup$
add a comment
|
$begingroup$
05AB1E, 10 bytes
h8jð0:2ôRJ
Try it online!
h convert input to hex
8j extend to length 8
ð0: replace spaces by zeros
2ô split in groups of two chars
R reverse groups
J and join all
$endgroup$
05AB1E, 10 bytes
h8jð0:2ôRJ
Try it online!
h convert input to hex
8j extend to length 8
ð0: replace spaces by zeros
2ô split in groups of two chars
R reverse groups
J and join all
answered 7 hours ago
DorianDorian
9714 silver badges6 bronze badges
9714 silver badges6 bronze badges
add a comment
|
add a comment
|
$begingroup$
JavaScript (ES7), 59 57 bytes
String manipulation.
n=>(n+2**32).toString(16).match(/B../g).reverse().join``
Try it online!
How?
We first convert $n + 2^{32}$ to hexadecimal to make sure that all leading $0$'s are included:
(304767 + 2**32).toString(16) // --> '10004a67f'
Try it online!
We use the regular expression /B../g
to match all groups of 2 digits, ignoring the leading $1$ thanks to B
(non-word boundary).
'10004a67f'.match(/B../g) // --> [ '00', '04', 'a6', '7f' ]
Try it online!
We reverse()
and join()
to get the final string.
JavaScript (ES6), 61 bytes
Recursive function.
f=(n,k=4)=>k?[(x=n&255)>>4&&'']+x.toString(16)+f(n>>8,k-1):''
Try it online!
$endgroup$
add a comment
|
$begingroup$
JavaScript (ES7), 59 57 bytes
String manipulation.
n=>(n+2**32).toString(16).match(/B../g).reverse().join``
Try it online!
How?
We first convert $n + 2^{32}$ to hexadecimal to make sure that all leading $0$'s are included:
(304767 + 2**32).toString(16) // --> '10004a67f'
Try it online!
We use the regular expression /B../g
to match all groups of 2 digits, ignoring the leading $1$ thanks to B
(non-word boundary).
'10004a67f'.match(/B../g) // --> [ '00', '04', 'a6', '7f' ]
Try it online!
We reverse()
and join()
to get the final string.
JavaScript (ES6), 61 bytes
Recursive function.
f=(n,k=4)=>k?[(x=n&255)>>4&&'']+x.toString(16)+f(n>>8,k-1):''
Try it online!
$endgroup$
add a comment
|
$begingroup$
JavaScript (ES7), 59 57 bytes
String manipulation.
n=>(n+2**32).toString(16).match(/B../g).reverse().join``
Try it online!
How?
We first convert $n + 2^{32}$ to hexadecimal to make sure that all leading $0$'s are included:
(304767 + 2**32).toString(16) // --> '10004a67f'
Try it online!
We use the regular expression /B../g
to match all groups of 2 digits, ignoring the leading $1$ thanks to B
(non-word boundary).
'10004a67f'.match(/B../g) // --> [ '00', '04', 'a6', '7f' ]
Try it online!
We reverse()
and join()
to get the final string.
JavaScript (ES6), 61 bytes
Recursive function.
f=(n,k=4)=>k?[(x=n&255)>>4&&'']+x.toString(16)+f(n>>8,k-1):''
Try it online!
$endgroup$
JavaScript (ES7), 59 57 bytes
String manipulation.
n=>(n+2**32).toString(16).match(/B../g).reverse().join``
Try it online!
How?
We first convert $n + 2^{32}$ to hexadecimal to make sure that all leading $0$'s are included:
(304767 + 2**32).toString(16) // --> '10004a67f'
Try it online!
We use the regular expression /B../g
to match all groups of 2 digits, ignoring the leading $1$ thanks to B
(non-word boundary).
'10004a67f'.match(/B../g) // --> [ '00', '04', 'a6', '7f' ]
Try it online!
We reverse()
and join()
to get the final string.
JavaScript (ES6), 61 bytes
Recursive function.
f=(n,k=4)=>k?[(x=n&255)>>4&&'']+x.toString(16)+f(n>>8,k-1):''
Try it online!
edited 8 hours ago
answered 8 hours ago
ArnauldArnauld
94k7 gold badges111 silver badges381 bronze badges
94k7 gold badges111 silver badges381 bronze badges
add a comment
|
add a comment
|
$begingroup$
Japt -P
, 10 bytes
sG ùT8 ò w
Try it
$endgroup$
$begingroup$
What does-P
do?
$endgroup$
– JL2210
4 hours ago
add a comment
|
$begingroup$
Japt -P
, 10 bytes
sG ùT8 ò w
Try it
$endgroup$
$begingroup$
What does-P
do?
$endgroup$
– JL2210
4 hours ago
add a comment
|
$begingroup$
Japt -P
, 10 bytes
sG ùT8 ò w
Try it
$endgroup$
Japt -P
, 10 bytes
sG ùT8 ò w
Try it
answered 6 hours ago
ShaggyShaggy
22k3 gold badges21 silver badges73 bronze badges
22k3 gold badges21 silver badges73 bronze badges
$begingroup$
What does-P
do?
$endgroup$
– JL2210
4 hours ago
add a comment
|
$begingroup$
What does-P
do?
$endgroup$
– JL2210
4 hours ago
$begingroup$
What does
-P
do?$endgroup$
– JL2210
4 hours ago
$begingroup$
What does
-P
do?$endgroup$
– JL2210
4 hours ago
add a comment
|
$begingroup$
Forth (gforth), 52 51 40 bytes
: f hex 0 4. do <# # # 0. #> type loop ;
Try it online!
Code explanation
: f start a new word definition
hex set the current base to base 16
0 convert the input number to a double-cell integer
4. do start a counted loop from 0 to 3
<# # # start a formatted numeric string and move last 2 digits to format area
0. move remaining digits down the stack
#> delete top two stack value and convert format area to string
type output string
loop end loop
; end word definition
$endgroup$
add a comment
|
$begingroup$
Forth (gforth), 52 51 40 bytes
: f hex 0 4. do <# # # 0. #> type loop ;
Try it online!
Code explanation
: f start a new word definition
hex set the current base to base 16
0 convert the input number to a double-cell integer
4. do start a counted loop from 0 to 3
<# # # start a formatted numeric string and move last 2 digits to format area
0. move remaining digits down the stack
#> delete top two stack value and convert format area to string
type output string
loop end loop
; end word definition
$endgroup$
add a comment
|
$begingroup$
Forth (gforth), 52 51 40 bytes
: f hex 0 4. do <# # # 0. #> type loop ;
Try it online!
Code explanation
: f start a new word definition
hex set the current base to base 16
0 convert the input number to a double-cell integer
4. do start a counted loop from 0 to 3
<# # # start a formatted numeric string and move last 2 digits to format area
0. move remaining digits down the stack
#> delete top two stack value and convert format area to string
type output string
loop end loop
; end word definition
$endgroup$
Forth (gforth), 52 51 40 bytes
: f hex 0 4. do <# # # 0. #> type loop ;
Try it online!
Code explanation
: f start a new word definition
hex set the current base to base 16
0 convert the input number to a double-cell integer
4. do start a counted loop from 0 to 3
<# # # start a formatted numeric string and move last 2 digits to format area
0. move remaining digits down the stack
#> delete top two stack value and convert format area to string
type output string
loop end loop
; end word definition
edited 5 hours ago
answered 7 hours ago
reffureffu
1,1613 silver badges7 bronze badges
1,1613 silver badges7 bronze badges
add a comment
|
add a comment
|
$begingroup$
C# (Visual C# Interactive Compiler), 58 bytes
x=>$"{((x=x>>16|x<<16)&4278255360)>>8|(x&16711935)<<8:x8}"
Try it online!
$endgroup$
add a comment
|
$begingroup$
C# (Visual C# Interactive Compiler), 58 bytes
x=>$"{((x=x>>16|x<<16)&4278255360)>>8|(x&16711935)<<8:x8}"
Try it online!
$endgroup$
add a comment
|
$begingroup$
C# (Visual C# Interactive Compiler), 58 bytes
x=>$"{((x=x>>16|x<<16)&4278255360)>>8|(x&16711935)<<8:x8}"
Try it online!
$endgroup$
C# (Visual C# Interactive Compiler), 58 bytes
x=>$"{((x=x>>16|x<<16)&4278255360)>>8|(x&16711935)<<8:x8}"
Try it online!
answered 5 hours ago
Embodiment of IgnoranceEmbodiment of Ignorance
5,6043 silver badges31 bronze badges
5,6043 silver badges31 bronze badges
add a comment
|
add a comment
|
$begingroup$
Jelly, 13 bytes
+Ø%b⁴Ḋs2Ṛ‘ịØh
Try it online!
A full program that takes an integer as its argument and prints a string.
$endgroup$
add a comment
|
$begingroup$
Jelly, 13 bytes
+Ø%b⁴Ḋs2Ṛ‘ịØh
Try it online!
A full program that takes an integer as its argument and prints a string.
$endgroup$
add a comment
|
$begingroup$
Jelly, 13 bytes
+Ø%b⁴Ḋs2Ṛ‘ịØh
Try it online!
A full program that takes an integer as its argument and prints a string.
$endgroup$
Jelly, 13 bytes
+Ø%b⁴Ḋs2Ṛ‘ịØh
Try it online!
A full program that takes an integer as its argument and prints a string.
answered 5 hours ago
Nick KennedyNick Kennedy
7,2391 gold badge9 silver badges18 bronze badges
7,2391 gold badge9 silver badges18 bronze badges
add a comment
|
add a comment
|
$begingroup$
APL+WIN, 36 34 bytes
2 bytes saved by converting to index zero
Prompts for integer:
'0123456789abcdef'[,⊖4 2⍴(8⍴16)⊤⎕]
Try it online! Courtesy Dyalog Classic
$endgroup$
add a comment
|
$begingroup$
APL+WIN, 36 34 bytes
2 bytes saved by converting to index zero
Prompts for integer:
'0123456789abcdef'[,⊖4 2⍴(8⍴16)⊤⎕]
Try it online! Courtesy Dyalog Classic
$endgroup$
add a comment
|
$begingroup$
APL+WIN, 36 34 bytes
2 bytes saved by converting to index zero
Prompts for integer:
'0123456789abcdef'[,⊖4 2⍴(8⍴16)⊤⎕]
Try it online! Courtesy Dyalog Classic
$endgroup$
APL+WIN, 36 34 bytes
2 bytes saved by converting to index zero
Prompts for integer:
'0123456789abcdef'[,⊖4 2⍴(8⍴16)⊤⎕]
Try it online! Courtesy Dyalog Classic
edited 5 hours ago
answered 5 hours ago
GrahamGraham
2,9267 silver badges8 bronze badges
2,9267 silver badges8 bronze badges
add a comment
|
add a comment
|
$begingroup$
C (gcc), 30 bytes
f(x){printf("%.8x",htonl(x));}
Try it online!
$endgroup$
$begingroup$
@ceilingcat That wouldn't be portable to big-endian platforms. I'll take it, though. Thanks!
$endgroup$
– JL2210
5 hours ago
add a comment
|
$begingroup$
C (gcc), 30 bytes
f(x){printf("%.8x",htonl(x));}
Try it online!
$endgroup$
$begingroup$
@ceilingcat That wouldn't be portable to big-endian platforms. I'll take it, though. Thanks!
$endgroup$
– JL2210
5 hours ago
add a comment
|
$begingroup$
C (gcc), 30 bytes
f(x){printf("%.8x",htonl(x));}
Try it online!
$endgroup$
C (gcc), 30 bytes
f(x){printf("%.8x",htonl(x));}
Try it online!
edited 5 hours ago
answered 6 hours ago
JL2210JL2210
2579 bronze badges
2579 bronze badges
$begingroup$
@ceilingcat That wouldn't be portable to big-endian platforms. I'll take it, though. Thanks!
$endgroup$
– JL2210
5 hours ago
add a comment
|
$begingroup$
@ceilingcat That wouldn't be portable to big-endian platforms. I'll take it, though. Thanks!
$endgroup$
– JL2210
5 hours ago
$begingroup$
@ceilingcat That wouldn't be portable to big-endian platforms. I'll take it, though. Thanks!
$endgroup$
– JL2210
5 hours ago
$begingroup$
@ceilingcat That wouldn't be portable to big-endian platforms. I'll take it, though. Thanks!
$endgroup$
– JL2210
5 hours ago
add a comment
|
$begingroup$
J, 10 bytes
8{._1{3!:3
Try it online!
$endgroup$
add a comment
|
$begingroup$
J, 10 bytes
8{._1{3!:3
Try it online!
$endgroup$
add a comment
|
$begingroup$
J, 10 bytes
8{._1{3!:3
Try it online!
$endgroup$
J, 10 bytes
8{._1{3!:3
Try it online!
answered 2 hours ago
JonahJonah
5,2072 gold badges13 silver badges24 bronze badges
5,2072 gold badges13 silver badges24 bronze badges
add a comment
|
add a comment
|
$begingroup$
Zsh, 46 bytes
i=$1
repeat 4 printf %02x $[j=i%256,i=i/256,j]
Try it online!
$endgroup$
add a comment
|
$begingroup$
Zsh, 46 bytes
i=$1
repeat 4 printf %02x $[j=i%256,i=i/256,j]
Try it online!
$endgroup$
add a comment
|
$begingroup$
Zsh, 46 bytes
i=$1
repeat 4 printf %02x $[j=i%256,i=i/256,j]
Try it online!
$endgroup$
Zsh, 46 bytes
i=$1
repeat 4 printf %02x $[j=i%256,i=i/256,j]
Try it online!
answered 2 hours ago
GammaFunctionGammaFunction
1,6503 silver badges15 bronze badges
1,6503 silver badges15 bronze badges
add a comment
|
add a comment
|
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
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%2fcodegolf.stackexchange.com%2fquestions%2f193793%2flittle-endian-num-2-string-conversion%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
$begingroup$
Does outputting to stdout count or does it have to be a string?
$endgroup$
– JL2210
6 hours ago
$begingroup$
@JL2210 if your technology not support strings, then yes, you can return result in any human readable way
$endgroup$
– Kamil Kiełczewski
6 hours ago
$begingroup$
Again, do you really need the emoji in the title?
$endgroup$
– Jo King
1 hour ago