www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Integer to hexadecimal string

reply jicman <cabrera_ _wrc.xerox.com> writes:
Greetings!

I know this has probably been asked before, or there is a simple way to do
this, but I can not find it.  I did a few searches on digitalmars on "decimal
to hexadecimal" and got not hit.

So, here is my problem: I am using a COM object that uses a file with filenames
on it and everything works fine until I have accented characters on the
filenames, such as é, Ñ, ä, etc.  So, what I am trying to do is to change those
filenames to URL style filename, such as,

c:\temp\my file\josé.doc

would change to,

c:\temp\my%20file\jos%e9.doc

So, I have two question:  Is there a function in phobos that does this
automatically?  If not, how do I change an integer, say, 233 to hex, E9?

I know how to do this programmatically, but there is gotta be an easier way
then me writing a function for this. :-)

thanks,

josé
Mar 11 2008
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to jicman,

 Greetings!
 
 I know this has probably been asked before, or there is a simple way
 to do this, but I can not find it.  I did a few searches on
 digitalmars on "decimal to hexadecimal" and got not hit.
 
 So, here is my problem: I am using a COM object that uses a file with
 filenames on it and everything works fine until I have accented
 characters on the filenames, such as é, Ñ, ä, etc.  So, what I am
 trying to do is to change those filenames to URL style filename, such
 as,
 
 c:\temp\my file\josé.doc
 
 would change to,
 
 c:\temp\my%20file\jos%e9.doc
 
 So, I have two question:  Is there a function in phobos that does this
 automatically?  If not, how do I change an integer, say, 233 to hex,
 E9?
 
 I know how to do this programmatically, but there is gotta be an
 easier way then me writing a function for this. :-)
 
it under URI. http://www.digitalmars.com/d/1.0/phobos/std_uri.html
 thanks,
 
 josé
 
Mar 11 2008
parent jicman <cabrera_ _wrc.xerox.com> writes:
BCS Wrote:

 Reply to jicman,
 
 Greetings!
 
 I know this has probably been asked before, or there is a simple way
 to do this, but I can not find it.  I did a few searches on
 digitalmars on "decimal to hexadecimal" and got not hit.
 
 So, here is my problem: I am using a COM object that uses a file with
 filenames on it and everything works fine until I have accented
 characters on the filenames, such as é, Ñ, ä, etc.  So, what I am
 trying to do is to change those filenames to URL style filename, such
 as,
 
 c:\temp\my file\josé.doc
 
 would change to,
 
 c:\temp\my%20file\jos%e9.doc
 
 So, I have two question:  Is there a function in phobos that does this
 automatically?  If not, how do I change an integer, say, 233 to hex,
 E9?
 
 I know how to do this programmatically, but there is gotta be an
 easier way then me writing a function for this. :-)
 
it under URI. http://www.digitalmars.com/d/1.0/phobos/std_uri.html
 thanks,
 
 josé
 
Thanks, but this does encodes but only does certain characters. No the ones I want like any accented or Spanish or, [any language, really,] characters. :-)
Mar 12 2008
prev sibling parent reply jicman <cabrera_ _wrc.xerox.com> writes:
jicman Wrote:

 Greetings!
 
 I know this has probably been asked before, or there is a simple way to do
this, but I can not find it.  I did a few searches on digitalmars on "decimal
to hexadecimal" and got not hit.
 
 So, here is my problem: I am using a COM object that uses a file with
filenames on it and everything works fine until I have accented characters on
the filenames, such as é, Ñ, ä, etc.  So, what I am trying to do is to change
those filenames to URL style filename, such as,
 
 c:\temp\my file\josé.doc
 
 would change to,
 
 c:\temp\my%20file\jos%e9.doc
 
 So, I have two question:  Is there a function in phobos that does this
automatically?  If not, how do I change an integer, say, 233 to hex, E9?
 
 I know how to do this programmatically, but there is gotta be an easier way
then me writing a function for this. :-)
 
 thanks,
 
 josé
Maybe I am going at this the wrong way... Let me explain what the problem is: I am using D to call a software that uses a Windows COM object to process a file which refers to a bunch of other files. This file which this COM object uses as input I create using D. This file is created by my doing a std.file.listdir(fd,"*") and then using those items returned from the listdir. This works perfectly and I can create the file to pass to the COM object which uses the file paths referred in the created file and everything works. However, when I have files with accented characters, I no longer have this working. The COM application fails with "It can not find the path given..." and it gives the path, which interestingly enough, is broken exactly where the accented or extended character is. I know what the problem is UTF8 vs. ANSI or one of those file kinds. If I open the file I create, I can see the problem. It is changing the file I created to ANSI, when it is really UTF8. This COM object, since it is Windows (.NET to be exact) does not allow me to set the input file, so it wants something else than UTF8, which is what I am creating with D. So, before I go and try to go through each of those filenames and rename then using URL base strings, is there something else that I can do to make this file be anything other than UTF8? Thanks for the help. Just point me to where to start reading... josé
Mar 11 2008
parent reply novice2 <sorry noem.ail> writes:
jicman Wrote:

other files.  This file which this COM object uses as input I create using D. 
>This file is created by my doing a std.file.listdir(fd,"*") and then using 
imho, you should use std.windows.charset.toMBSz() while you pass string from D to Windows API, and std.windows.charset.fromMBSz() while you pass string back from Windows API to D
Mar 12 2008
parent reply jicman <cabrera_ _wrc.xerox.com> writes:
novice2 Wrote:

 jicman Wrote:
 
other files.  This file which this COM object uses as input I create using D.  
This file is created by my doing a std.file.listdir(fd,"*") and then using 
imho, you should use std.windows.charset.toMBSz() while you pass string from D to Windows API, and std.windows.charset.fromMBSz() while you pass string back from Windows API to D
Is this part of Phobos 1.0? I only see in in Phobos 2.0. Is there a similar call within Phobos 1.0?
Mar 12 2008
parent reply Dave Akers <dragon dazoe.net> writes:
jicman wrote:
 novice2 Wrote:
 
 jicman Wrote:

 other files.  This file which this COM object uses as input I create using D.  
 This file is created by my doing a std.file.listdir(fd,"*") and then using 
imho, you should use std.windows.charset.toMBSz() while you pass string from D to Windows API, and std.windows.charset.fromMBSz() while you pass string back from Windows API to D
Is this part of Phobos 1.0? I only see in in Phobos 2.0. Is there a similar call within Phobos 1.0?
why not use format?? char[] hex = format("%.8x", 12345); that produces hex.. import std.string; char[] toHex(T)(T arg){ return format("%."~std.string.toString(T.sizeof * 2)~"x", arg); } i haven't tested that code. but i think it'd work for ulong long uint int ubyte and byte. -dave
Jul 08 2008
parent reply BCS <ao pathlink.com> writes:
Reply to Dave,

 jicman wrote:
 
 novice2 Wrote:
 
 jicman Wrote:
 
 other files.  This file which this COM object uses as input I
 create using D.  This file is created by my doing a
 std.file.listdir(fd,"*") and then using
 
imho, you should use std.windows.charset.toMBSz() while you pass string from D to Windows API, and std.windows.charset.fromMBSz() while you pass string back from Windows API to D
Is this part of Phobos 1.0? I only see in in Phobos 2.0. Is there a similar call within Phobos 1.0?
why not use format?? char[] hex = format("%.8x", 12345); that produces hex.. import std.string; char[] toHex(T)(T arg){ return format("%."~std.string.toString(T.sizeof * 2)~"x", arg); } i haven't tested that code. but i think it'd work for ulong long uint int ubyte and byte. -dave
If you steal some code from Don Clugston: http://www.dsource.org/projects/ddl/browser/trunk/meta/conv.d template decimaldigit(int n) { const char [] decimaldigit = "0123456789"[n..n+1]; } template itoa(long n) { static if (n<0) const char [] itoa = "-" ~ itoa!(-n); else static if (n<10L) const char [] itoa = decimaldigit!(n); else const char [] itoa = itoa!(n/10L) ~ decimaldigit!(n%10L); } you can even force the format string to be built at compile time. char[] toHex(T)(T arg){ return format("%."~itoa!(T.sizeof * 2)~"x", arg); }
Jul 08 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"BCS" <ao pathlink.com> wrote in message 
news:55391cb32eed78caaf3194222520 news.digitalmars.com...

 you can even force the format string to be built at compile time.

 char[] toHex(T)(T arg){ return format("%."~itoa!(T.sizeof * 2)~"x", 
 arg); }
No function necessary. return format("%." ~ (T.sizeof * 2).stringof ~ "x", arg); Untested, but you can use .stringof to do number to string at compile time. Not sure if it'll give "T.sizeof * 2" instead here :P might have to put it in a const.
Jul 08 2008
parent reply BCS <ao pathlink.com> writes:
Reply to Jarrett,

 "BCS" <ao pathlink.com> wrote in message
 news:55391cb32eed78caaf3194222520 news.digitalmars.com...
 
 you can even force the format string to be built at compile time.
 
 char[] toHex(T)(T arg){ return format("%."~itoa!(T.sizeof * 2)~"x",
 arg); }
 
No function necessary. return format("%." ~ (T.sizeof * 2).stringof ~ "x", arg); Untested, but you can use .stringof to do number to string at compile time. Not sure if it'll give "T.sizeof * 2" instead here :P might have to put it in a const.
I think that won't work. The only place I know that .stringof works that way is __LINE__.stringof but that's special cased in the lexer IIRC.
Jul 09 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"BCS" <ao pathlink.com> wrote in message 
news:55391cb32ef558caafbcbd0e7af2 news.digitalmars.com...
 Reply to Jarrett,

 "BCS" <ao pathlink.com> wrote in message
 news:55391cb32eed78caaf3194222520 news.digitalmars.com...

 you can even force the format string to be built at compile time.

 char[] toHex(T)(T arg){ return format("%."~itoa!(T.sizeof * 2)~"x",
 arg); }
No function necessary. return format("%." ~ (T.sizeof * 2).stringof ~ "x", arg); Untested, but you can use .stringof to do number to string at compile time. Not sure if it'll give "T.sizeof * 2" instead here :P might have to put it in a const.
I think that won't work. The only place I know that .stringof works that way is __LINE__.stringof but that's special cased in the lexer IIRC.
Just try it, please? template Itoa(int x) { const Itoa = x.stringof; } pragma(msg, (5).stringof); // "5" pragma(msg, (3 + 4).stringof); // "3 + 4" pragma(msg, Itoa!(3 + 4)); // "7"
Jul 09 2008
parent BCS <ao pathlink.com> writes:
Reply to Jarrett,

 "BCS" <ao pathlink.com> wrote in message
 news:55391cb32ef558caafbcbd0e7af2 news.digitalmars.com...
 
 Reply to Jarrett,
 
 "BCS" <ao pathlink.com> wrote in message
 news:55391cb32eed78caaf3194222520 news.digitalmars.com...
 you can even force the format string to be built at compile time.
 
 char[] toHex(T)(T arg){ return format("%."~itoa!(T.sizeof * 2)~"x",
 arg); }
 
No function necessary. return format("%." ~ (T.sizeof * 2).stringof ~ "x", arg); Untested, but you can use .stringof to do number to string at compile time. Not sure if it'll give "T.sizeof * 2" instead here :P might have to put it in a const.
I think that won't work. The only place I know that .stringof works that way is __LINE__.stringof but that's special cased in the lexer IIRC.
Just try it, please? template Itoa(int x) { const Itoa = x.stringof; } pragma(msg, (5).stringof); // "5" pragma(msg, (3 + 4).stringof); // "3 + 4" pragma(msg, Itoa!(3 + 4)); // "7"
I stand corrected: It didn't work last time I tried it. (I think I only managed to test case 2 & 1but with consts)
Jul 09 2008