Convert a number to hex string

8,482 views
Skip to first unread message

Davy Mitchell

unread,
Jun 24, 2013, 7:03:22 AM6/24/13
to mi...@dartlang.org
Hi Folks,

I'm looking to convert an int to a hex string. Neither num, int or string seem to have a suitable method.

There appears to be a method in the library fixednum's int32 class but that library deprecated apparently? Still in API docs tho.

Apologies if I am missing something obvious.

Thanks,
Davy

William Hesse

unread,
Jun 24, 2013, 7:17:43 AM6/24/13
to General Dart Discussion
int.toRadixString(16) should convert an int to a hex string, though it does not add a leading 0x.
Then String.toUpperCase can be used if you want A..F instead of a..f to represent the hex digits.



--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new
 
 



--
William Hesse

Davy Mitchell

unread,
Jun 24, 2013, 7:31:51 AM6/24/13
to mi...@dartlang.org
Thanks for the fast reply William. Looks like what I need.

Message has been deleted

Davy Mitchell

unread,
Jun 24, 2013, 3:35:25 PM6/24/13
to mi...@dartlang.org
Thanks for that. I was thinking this was easier in other languages but looks about the same as .Net/Python. Confusing having fixednum in the docs though.

*ponders feature request and/or bug*

:-)

Davy


On 24 June 2013 13:10, mezoni <andrew...@gmail.com> wrote:
Here are more advanced example.

void main() { var unicode = charToUnicode(16); print(unicode); } String charToUnicode(int char) { if(char == null || char < 0 || char > 0xfffffffff) { throw new ArgumentError('c: $char'); } var hex = char.toRadixString(16); var length = hex.length; var count = 0; if(char <= 0xffff) { count = 4; } else { count = 8; } for(var i = 0; i < count - length; i++) { hex = '0$hex'; } return '\\u$hex'; }

Output:
\u0010

Alex Tatumizer

unread,
Jun 24, 2013, 10:27:09 PM6/24/13
to mi...@dartlang.org
String charToUnicodeString(int char) =>
 
(char < 0 || char > 0xFFFFFF) ? throw new ArgumentError("out of Unicode range: $char") :  
 
char <= 0xFFFF ? "\\u${(char+0x10000).toRadixString(16).substring(1)}" :
 
"\\u{${char.toRadixString(16)}}";



I wonder if this is the shortest way to write it.
 

Lasse R.H. Nielsen

unread,
Jun 25, 2013, 8:38:30 AM6/25/13
to mi...@dartlang.org
Shortest generating code or shortest output? 
The shortest output is \x.. for values < 256, \uxxxx for values < 65536 and \u{......} for the rest.
For generating code it's pretty good, but not as short as "\\u{${char.toRadixString(16)}}" :)

/L


 

--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new
 
 



--
Lasse R.H. Nielsen - l...@google.com  
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K - Denmark - CVR nr. 28 86 69 84

Alex Tatumizer

unread,
Jun 25, 2013, 1:26:22 PM6/25/13
to mi...@dartlang.org
That's what happens when one tries to solve unspecified problem :-)

There's a lot in dart itself and its libraries that suffers from the same malaise. Aforementioned "const" is an illustration :-)

Anyway, snippet takes advantage of very good feature of dart: you can throw exceptions in expressions.
this makes possible to write the whole if-else if-else if-... as a single expression, using ?: operator.
(Another feature that helps constructing cool expressions is double dot - really good idea).

Thanks for \x tip, I didn't know about it.

Reply all
Reply to author
Forward
0 new messages