View mode: basic / threaded / horizontal-split · Log in · Help
October 09, 2004
ubyte to string conversion (md5)
well,
i'm kinda stuck.. i've done those things before, and for some reason, they 
won't work here..
i'm trying to convert an MD5 digest from std.md5 (ubyte[16]) into a string 
(char[])
i need it for internal usage, so using printDigest is useless for me, unless 
there is a way i can print it into a string..
i tired (fuitily i must say) several ways of converting the bytes into 
chars,
none worked.

does anyone have any code for that, or some tips for me ?

Cheers,
Asaf
October 10, 2004
Re: ubyte to string conversion (md5)
"Asaf Karagila" <kas1@netvision.net.il> wrote in message
news:ck9s27$1b4a$1@digitaldaemon.com...
> well,
> i'm kinda stuck.. i've done those things before, and for some reason, they
> won't work here..
> i'm trying to convert an MD5 digest from std.md5 (ubyte[16]) into a string
> (char[])
> i need it for internal usage, so using printDigest is useless for me,
unless
> there is a way i can print it into a string..
> i tired (fuitily i must say) several ways of converting the bytes into
> chars,
> none worked.
>
> does anyone have any code for that, or some tips for me ?
>
> Cheers,
> Asaf
>
>

do you mean something like:

import std.string;
import std.stdio;
ubyte[] x = [10, 20, 30];
int main() {
 char[] y;
 foreach(ubyte n;x)
   y ~= toString(n) ~ " ";
 writefln(y);
 return 0;
}
October 10, 2004
Re: ubyte to string conversion (md5)
"Ben Hinkle" <bhinkle@mathworks.com> wrote in message 
news:cka2o8$1ep3$1@digitaldaemon.com...
>
> "Asaf Karagila" <kas1@netvision.net.il> wrote in message
> news:ck9s27$1b4a$1@digitaldaemon.com...
>> well,
>> i'm kinda stuck.. i've done those things before, and for some reason, 
>> they
>> won't work here..
>> i'm trying to convert an MD5 digest from std.md5 (ubyte[16]) into a 
>> string
>> (char[])
>> i need it for internal usage, so using printDigest is useless for me,
> unless
>> there is a way i can print it into a string..
>> i tired (fuitily i must say) several ways of converting the bytes into
>> chars,
>> none worked.
>>
>> does anyone have any code for that, or some tips for me ?
>>
>> Cheers,
>> Asaf
>>
>>
>
> do you mean something like:
>
> import std.string;
> import std.stdio;
> ubyte[] x = [10, 20, 30];
> int main() {
>  char[] y;
>  foreach(ubyte n;x)
>    y ~= toString(n) ~ " ";
>  writefln(y);
>  return 0;
> }
>
>

thats a cool routine, but its not exactly what i am looking for,
it gives out a decimal output, what i need is actually a 128bit hex number 
output..
October 10, 2004
Re: ubyte to string conversion (md5)
On Sun, 10 Oct 2004 01:26:51 +0200, Asaf Karagila <kas1@netvision.net.il> 
wrote:
> well,
> i'm kinda stuck.. i've done those things before, and for some reason, 
> they
> won't work here..
> i'm trying to convert an MD5 digest from std.md5 (ubyte[16]) into a 
> string
> (char[])
> i need it for internal usage, so using printDigest is useless for me, 
> unless
> there is a way i can print it into a string..
> i tired (fuitily i must say) several ways of converting the bytes into
> chars,
> none worked.
>
> does anyone have any code for that, or some tips for me ?

Try this...

template hexStringT(T)
{
	char[] hexStringT(T d)
	{
		char[] result;

		if (d.length != 0) {
			typeof(d[0]) u;
			uint sz = u.sizeof*2;
			uint ndigits = 0;

			result = new char[sz*d.length];
			for(int i = d.length-1; i >= 0; i--) {			
				u = d[i];
				for(; u; u /= 16) {
					result[result.length-1-ndigits] = hexdigits[u & 15];
					ndigits++;
				}
				for(; ndigits < (d.length-i)*sz; ndigits++)
					result[result.length-1-ndigits] = '0';
			}
		}

		return result;
	}
}

char[] toHexString(ubyte[] array)
{
	return hexStringT!(typeof(array))(array);
}

char[] toHexString(ulong[] array)
{
	return hexStringT!(typeof(array))(array);
}

char[] toHexString(uint[] array)
{
	return hexStringT!(typeof(array))(array);
}

The above code is from my library of hashing routines in the Deimos 
library which you can find here:

http://www.dsource.org/projects/deimos/

Deimos is an open source library to which anyone can contribute. You can 
use the "Browse SVN Repository for Deimos" link to have a look at the 
other source files, the hashing routines are here:

http://svn.dsource.org/svn/projects/deimos/trunk/etc/crypto/hash/

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
October 11, 2004
Re: ubyte to string conversion (md5)
thanks for the code,
its a bit over my level at the moment, and i don't like using code i can't 
understand..
so i'm not sure if i'll use it,
as well, i managed to sort something out.. its almost perfect as well..

foreach(ubyte u;array)
    myString ~= std.string.format("%2X",u);

it gives me out a pretty good result, only its sticking a 'b' on the end, i 
don't know why..
anyone got a clue about that ?

Cheers,
Asaf
October 11, 2004
Re: ubyte to string conversion (md5)
Asaf Karagila wrote:
> thanks for the code,
> its a bit over my level at the moment, and i don't like using code i can't 
> understand..
> so i'm not sure if i'll use it,
> as well, i managed to sort something out.. its almost perfect as well..
> 
> foreach(ubyte u;array)
>      myString ~= std.string.format("%2X",u);
> 
> it gives me out a pretty good result, only its sticking a 'b' on the end, i 
> don't know why..
> anyone got a clue about that ?
> 
> Cheers,
> Asaf 
> 
> 

I don't quite see what the problem is. If I run the following code:

import std.stdio;
import std.string;

int main (char[][] args) {

	char[] s;
	const static ubyte[] array = [
            0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
	    0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF
        ];
	foreach(ubyte u; array) {
		s ~= format("%2X", u);
	}
	
	writefln(s); // writes: 0 1 2 3 4 5 6 7 8 9 A B C D E F
	
    return 0;
}

It does print the values as it is supposed to do

Regards,
Sjoerd
October 11, 2004
Re: ubyte to string conversion (md5)
"Sjoerd van Leent" <svanleent@wanadoo.nl> wrote in message 
news:ckdp2m$15id$1@digitaldaemon.com...
> Asaf Karagila wrote:
>> thanks for the code,
>> its a bit over my level at the moment, and i don't like using code i 
>> can't understand..
>> so i'm not sure if i'll use it,
>> as well, i managed to sort something out.. its almost perfect as well..
>>
>> foreach(ubyte u;array)
>>      myString ~= std.string.format("%2X",u);
>>
>> it gives me out a pretty good result, only its sticking a 'b' on the end, 
>> i don't know why..
>> anyone got a clue about that ?
>>
>> Cheers,
>> Asaf
>
> I don't quite see what the problem is. If I run the following code:
>
> import std.stdio;
> import std.string;
>
> int main (char[][] args) {
>
> char[] s;
> const static ubyte[] array = [
>             0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
>     0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF
>         ];
> foreach(ubyte u; array) {
> s ~= format("%2X", u);
> }
>
> writefln(s); // writes: 0 1 2 3 4 5 6 7 8 9 A B C D E F
>
>     return 0;
> }
>
> It does print the values as it is supposed to do
>
> Regards,
> Sjoerd

yeah, it doesn't look flawed either, so maybe the buffer allocated for the 
char[] i have is already used ?
if so, is there a way to initialize the array to be clear, and to clean the 
memory its using ?

Cheers,
Asaf
October 11, 2004
Re: ubyte to string conversion (md5)
Asaf Karagila wrote:
> "Sjoerd van Leent" <svanleent@wanadoo.nl> wrote in message 
> news:ckdp2m$15id$1@digitaldaemon.com...
> 
>>Asaf Karagila wrote:
>>
>>>thanks for the code,
>>>its a bit over my level at the moment, and i don't like using code i 
>>>can't understand..
>>>so i'm not sure if i'll use it,
>>>as well, i managed to sort something out.. its almost perfect as well..
>>>
>>>foreach(ubyte u;array)
>>>     myString ~= std.string.format("%2X",u);
>>>
>>>it gives me out a pretty good result, only its sticking a 'b' on the end, 
>>>i don't know why..
>>>anyone got a clue about that ?
>>>
>>>Cheers,
>>>Asaf
>>
>>I don't quite see what the problem is. If I run the following code:
>>
>>import std.stdio;
>>import std.string;
>>
>>int main (char[][] args) {
>>
>>char[] s;
>>const static ubyte[] array = [
>>            0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
>>    0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF
>>        ];
>>foreach(ubyte u; array) {
>>s ~= format("%2X", u);
>>}
>>
>>writefln(s); // writes: 0 1 2 3 4 5 6 7 8 9 A B C D E F
>>
>>    return 0;
>>}
>>
>>It does print the values as it is supposed to do
>>
>>Regards,
>>Sjoerd
> 
> 
> yeah, it doesn't look flawed either, so maybe the buffer allocated for the 
> char[] i have is already used ?
> if so, is there a way to initialize the array to be clear, and to clean the 
> memory its using ?
> 
> Cheers,
> Asaf 
> 
> 

Initialize the array to an empty array. This could be done  using an 
empty array or using the init property of the current array:

char[] s;
s = s.init;

this should work as expected.

Regards,
Sjoerd
October 11, 2004
Re: ubyte to string conversion (md5)
"Sjoerd van Leent" <svanleent@wanadoo.nl> wrote in message 
news:ckebb5$1mkb$1@digitaldaemon.com...
> Asaf Karagila wrote:
>> "Sjoerd van Leent" <svanleent@wanadoo.nl> wrote in message 
>> news:ckdp2m$15id$1@digitaldaemon.com...
>>
>>>Asaf Karagila wrote:
>>>
>>>>thanks for the code,
>>>>its a bit over my level at the moment, and i don't like using code i 
>>>>can't understand..
>>>>so i'm not sure if i'll use it,
>>>>as well, i managed to sort something out.. its almost perfect as well..
>>>>
>>>>foreach(ubyte u;array)
>>>>     myString ~= std.string.format("%2X",u);
>>>>
>>>>it gives me out a pretty good result, only its sticking a 'b' on the 
>>>>end, i don't know why..
>>>>anyone got a clue about that ?
>>>>
>>>>Cheers,
>>>>Asaf
>>>
>>>I don't quite see what the problem is. If I run the following code:
>>>
>>>import std.stdio;
>>>import std.string;
>>>
>>>int main (char[][] args) {
>>>
>>>char[] s;
>>>const static ubyte[] array = [
>>>            0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
>>>    0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF
>>>        ];
>>>foreach(ubyte u; array) {
>>>s ~= format("%2X", u);
>>>}
>>>
>>>writefln(s); // writes: 0 1 2 3 4 5 6 7 8 9 A B C D E F
>>>
>>>    return 0;
>>>}
>>>
>>>It does print the values as it is supposed to do
>>>
>>>Regards,
>>>Sjoerd
>>
>>
>> yeah, it doesn't look flawed either, so maybe the buffer allocated for 
>> the char[] i have is already used ?
>> if so, is there a way to initialize the array to be clear, and to clean 
>> the memory its using ?
>>
>> Cheers,
>> Asaf
>
> Initialize the array to an empty array. This could be done  using an empty 
> array or using the init property of the current array:
>
> char[] s;
> s = s.init;
>
> this should work as expected.
>
> Regards,
> Sjoerd
>

   char[] MD5;
   MD5.init;
   foreach(ubyte u;di)
       MD5 ~= std.string.format("%2X",u);
   printf(MD5);

the output is :
"9ADD3AF83DD9DAAA75BDF5CE4E476CBCb"
i don't know where does this 'b' is coming from, and i would like to remove 
it..
weird.

Cheers,
Asaf
October 11, 2004
Re: ubyte to string conversion (md5)
Asaf Karagila wrote:
> "Sjoerd van Leent" <svanleent@wanadoo.nl> wrote in message 
> news:ckebb5$1mkb$1@digitaldaemon.com...
> 
>>Asaf Karagila wrote:
>>
>>>"Sjoerd van Leent" <svanleent@wanadoo.nl> wrote in message 
>>>news:ckdp2m$15id$1@digitaldaemon.com...
>>>
>>>
>>>>Asaf Karagila wrote:
>>>>
>>>>
>>>>>thanks for the code,
>>>>>its a bit over my level at the moment, and i don't like using code i 
>>>>>can't understand..
>>>>>so i'm not sure if i'll use it,
>>>>>as well, i managed to sort something out.. its almost perfect as well..
>>>>>
>>>>>foreach(ubyte u;array)
>>>>>    myString ~= std.string.format("%2X",u);
>>>>>
>>>>>it gives me out a pretty good result, only its sticking a 'b' on the 
>>>>>end, i don't know why..
>>>>>anyone got a clue about that ?
>>>>>
>>>>>Cheers,
>>>>>Asaf
>>>>
>>>>I don't quite see what the problem is. If I run the following code:
>>>>
>>>>import std.stdio;
>>>>import std.string;
>>>>
>>>>int main (char[][] args) {
>>>>
>>>>char[] s;
>>>>const static ubyte[] array = [
>>>>           0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
>>>>   0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF
>>>>       ];
>>>>foreach(ubyte u; array) {
>>>>s ~= format("%2X", u);
>>>>}
>>>>
>>>>writefln(s); // writes: 0 1 2 3 4 5 6 7 8 9 A B C D E F
>>>>
>>>>   return 0;
>>>>}
>>>>
>>>>It does print the values as it is supposed to do
>>>>
>>>>Regards,
>>>>Sjoerd
>>>
>>>
>>>yeah, it doesn't look flawed either, so maybe the buffer allocated for 
>>>the char[] i have is already used ?
>>>if so, is there a way to initialize the array to be clear, and to clean 
>>>the memory its using ?
>>>
>>>Cheers,
>>>Asaf
>>
>>Initialize the array to an empty array. This could be done  using an empty 
>>array or using the init property of the current array:
>>
>>char[] s;
>>s = s.init;
>>
>>this should work as expected.
>>
>>Regards,
>>Sjoerd
>>
> 
> 
>     char[] MD5;
>     MD5.init;
>     foreach(ubyte u;di)
>         MD5 ~= std.string.format("%2X",u);
>     printf(MD5);
> 
> the output is :
> "9ADD3AF83DD9DAAA75BDF5CE4E476CBCb"
> i don't know where does this 'b' is coming from, and i would like to remove 
> it..
> weird.

http://www.digitalmars.com/d/faq.html#printf
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home