digitalmars.D.learn - md5 return toHexString
- AndyC (26/26) Apr 24 2015 Hi All, I cannot seem to understand whats wrong with this:
- tcak (4/30) Apr 24 2015 Just do that "return s.dup()". Then it works for me. That's
- AndyC (8/48) Apr 24 2015 Ah, yep, that works. I'd originally written it as:
- Johannes Pfau (5/60) Apr 24 2015 https://issues.dlang.org/show_bug.cgi?id=9279
Hi All, I cannot seem to understand whats wrong with this: // main.d import std.stdio; import std.digest.md; import std.file; string md5sum(const string fname) { MD5 hash; File f = File(fname, "rb"); foreach( ubyte[] buf; f.byChunk(4096)) { hash.put(buf); } string s = toHexString!(LetterCase.lower)(hash.finish()); writeln(s); //This is correct return s; } void main() { string crc = md5sum("main.d"); writeln(crc); //This is garbage } The writeln in md5sum prints correctly, but the "return s" seems to mess it up? What's going on? Thanks for you time, -Andy
Apr 24 2015
On Friday, 24 April 2015 at 17:50:03 UTC, AndyC wrote:Hi All, I cannot seem to understand whats wrong with this: // main.d import std.stdio; import std.digest.md; import std.file; string md5sum(const string fname) { MD5 hash; File f = File(fname, "rb"); foreach( ubyte[] buf; f.byChunk(4096)) { hash.put(buf); } string s = toHexString!(LetterCase.lower)(hash.finish()); writeln(s); //This is correct return s; } void main() { string crc = md5sum("main.d"); writeln(crc); //This is garbage } The writeln in md5sum prints correctly, but the "return s" seems to mess it up? What's going on? Thanks for you time, -AndyJust do that "return s.dup()". Then it works for me. That's probably due to the fact that s is in stack. But I am not sure how "toHexString" works.
Apr 24 2015
On Friday, 24 April 2015 at 17:56:59 UTC, tcak wrote:On Friday, 24 April 2015 at 17:50:03 UTC, AndyC wrote:Ah, yep, that works. I'd originally written it as: return toHexString!(LetterCase.lower)(hash.finish()); Which doesn't work, so used the temp string to test it. Now I'm using: return toHexString!(LetterCase.lower)(hash.finish()).dup(); Kinda weird. But works. Thank you! -AndyHi All, I cannot seem to understand whats wrong with this: // main.d import std.stdio; import std.digest.md; import std.file; string md5sum(const string fname) { MD5 hash; File f = File(fname, "rb"); foreach( ubyte[] buf; f.byChunk(4096)) { hash.put(buf); } string s = toHexString!(LetterCase.lower)(hash.finish()); writeln(s); //This is correct return s; } void main() { string crc = md5sum("main.d"); writeln(crc); //This is garbage } The writeln in md5sum prints correctly, but the "return s" seems to mess it up? What's going on? Thanks for you time, -AndyJust do that "return s.dup()". Then it works for me. That's probably due to the fact that s is in stack. But I am not sure how "toHexString" works.
Apr 24 2015
Am Fri, 24 Apr 2015 18:02:57 +0000 schrieb "AndyC" <andy squeakycode.net>:On Friday, 24 April 2015 at 17:56:59 UTC, tcak wrote:https://issues.dlang.org/show_bug.cgi?id=9279 toHexstring doesn't return a string, it returns char[n], a fixed-size array of length n. It shouldn't implicitly convert to a string.On Friday, 24 April 2015 at 17:50:03 UTC, AndyC wrote:Ah, yep, that works. I'd originally written it as: return toHexString!(LetterCase.lower)(hash.finish()); Which doesn't work, so used the temp string to test it. Now I'm using: return toHexString!(LetterCase.lower)(hash.finish()).dup(); Kinda weird. But works. Thank you! -AndyHi All, I cannot seem to understand whats wrong with this: // main.d import std.stdio; import std.digest.md; import std.file; string md5sum(const string fname) { MD5 hash; File f = File(fname, "rb"); foreach( ubyte[] buf; f.byChunk(4096)) { hash.put(buf); } string s = toHexString!(LetterCase.lower)(hash.finish()); writeln(s); //This is correct return s; } void main() { string crc = md5sum("main.d"); writeln(crc); //This is garbage } The writeln in md5sum prints correctly, but the "return s" seems to mess it up? What's going on? Thanks for you time, -AndyJust do that "return s.dup()". Then it works for me. That's probably due to the fact that s is in stack. But I am not sure how "toHexString" works.
Apr 24 2015