Jump to page: 1 2
Thread overview
Integer to hexadecimal string
Mar 12, 2008
jicman
Mar 12, 2008
BCS
Mar 13, 2008
jicman
Mar 12, 2008
jicman
Mar 12, 2008
novice2
Mar 13, 2008
jicman
Jul 08, 2008
Dave Akers
Jul 08, 2008
BCS
Jul 09, 2008
BCS
Jul 09, 2008
BCS
March 12, 2008
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é
March 12, 2008
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é
> 


March 12, 2008
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é

March 12, 2008
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
March 13, 2008
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?
March 13, 2008
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. :-)

July 08, 2008
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
July 08, 2008
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); }


July 09, 2008
"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.


July 09, 2008
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.


« First   ‹ Prev
1 2