www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - String to int exception

reply "Alexandre" <alebencz gmail.com> writes:
Hi :)

I made this function to inverse the bytes in intger or T 
(possible) type...

int reverseBytes(T)(T val)
{
	int retVal = 0;
	static if(is(T == string))
		retVal = to!int(val);

	return (retVal & 0x000000FF) << 24 |
		   (retVal & 0x0000FF00) << 8 |
		   (retVal & 0x00FF0000) >> 8 |
		   (retVal & 0xFF000000) >> 24;
}

//...
writefln("%x", reverseBytes(x"00402030"));
//...

When I execute this program, I got this exception:
std.conv.ConvException C:\D\dmd2\src\phobos\std\conv.d(1968): 
Unexpected ' ' when converting from type string to type int
Jul 15 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Alexandre:

 	return (retVal & 0x000000FF) << 24 |
 		   (retVal & 0x0000FF00) << 8 |
 		   (retVal & 0x00FF0000) >> 8 |
 		   (retVal & 0xFF000000) >> 24;
See also core.bitop.bswap. Bye, bearophile
Jul 15 2014
parent reply "Alexandre" <alebencz gmail.com> writes:
Thanks, but, when I convert I recive a 'c' in the front of my 
number...

uint reverseBytes(uint val)
{
	import core.bitop : bitswap;

	return bitswap(val);
}

//...
writefln("%x", reverseBytes(0x00402030));
//...

// output: c040200

On Tuesday, 15 July 2014 at 12:16:26 UTC, bearophile wrote:
 See also core.bitop.bswap.

 Bye,
 bearophile
Jul 15 2014
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Alexandre:

 Thanks, but, when I convert I recive a 'c' in the front of my 
 number...
This shows it inverts all bits, not just the four byte positions. I don't understand: import core.bitop: bitswap; uint reverseBytes(in uint val) pure nothrow safe nogc { return val.bitswap; } void main() { import std.stdio; immutable uint x = 0x_00_40_20_30U; immutable uint y = x.reverseBytes; writefln("%08x", x); writefln("%032b", x); writefln("%08x", y); writefln("%032b", y); } Bye, bearophile
Jul 15 2014
parent "Alexandre" <alebencz gmail.com> writes:
Something is wrong between our communication...

I am wanting to do something better to order the bytes, for this 
my code...
https://gist.github.com/bencz/3576dfc8a217a34c05a9

For example, in that case:
injectData(image[0x207], x"30204000");

It's more simple to use something like:
injectData(image[0x207], inverseBytes(0x00402030));


On Tuesday, 15 July 2014 at 13:15:34 UTC, bearophile wrote:
 Alexandre:

 Thanks, but, when I convert I recive a 'c' in the front of my 
 number...
This shows it inverts all bits, not just the four byte positions. I don't understand: import core.bitop: bitswap; uint reverseBytes(in uint val) pure nothrow safe nogc { return val.bitswap; } void main() { import std.stdio; immutable uint x = 0x_00_40_20_30U; immutable uint y = x.reverseBytes; writefln("%08x", x); writefln("%032b", x); writefln("%08x", y); writefln("%032b", y); } Bye, bearophile
Jul 15 2014
prev sibling parent "safety0ff" <safety0ff.dev gmail.com> writes:
On Tuesday, 15 July 2014 at 12:24:48 UTC, Alexandre wrote:
 Thanks, but, when I convert I recive a 'c' in the front of my 
 number...

 uint reverseBytes(uint val)
 {
 	import core.bitop : bitswap;

 	return bitswap(val);
 }
You confused bswap with bitswap. The former reverses bytes, the latter reverses bits. If you look at bearophile's original message he says bswap.
Jul 15 2014
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 07/15/2014 04:56 AM, Alexandre wrote:

          retVal = to!int(val);
 std.conv.ConvException C:\D\dmd2\src\phobos\std\conv.d(1968): Unexpected
 ' ' when converting from type string to type int
That means to!int failed because 'val' contained a ' ' character in it: import std.conv; void main() { auto s = "42 "; // <-- What is that? auto i = to!int(s); } However, there seems to be a bug in to(). It seems to incorrectly go one character ahead: to!int("mn"); "Unexpected 'n' when converting from type string to type int" to!int("m"); "Unexpected end of input when converting from type string to type int" That is a bug, right? So, in your case the unexpected ' ' character may be after another unexpected one: to!int(" "); "Unexpected ' ' when converting from type string to type int" Ali
Jul 15 2014
parent reply "Alexandre" <alebencz gmail.com> writes:
Strange..., why ' ' ?

PS: Ali Çehreli, thanks for your book, your book is wonderful!!!

On Tuesday, 15 July 2014 at 13:49:51 UTC, Ali Çehreli wrote:
 On 07/15/2014 04:56 AM, Alexandre wrote:

          retVal = to!int(val);
 std.conv.ConvException C:\D\dmd2\src\phobos\std\conv.d(1968):
Unexpected
 ' ' when converting from type string to type int
That means to!int failed because 'val' contained a ' ' character in it: import std.conv; void main() { auto s = "42 "; // <-- What is that? auto i = to!int(s); } However, there seems to be a bug in to(). It seems to incorrectly go one character ahead: to!int("mn"); "Unexpected 'n' when converting from type string to type int" to!int("m"); "Unexpected end of input when converting from type string to type int" That is a bug, right? So, in your case the unexpected ' ' character may be after another unexpected one: to!int(" "); "Unexpected ' ' when converting from type string to type int" Ali
Jul 15 2014
parent "anonymous" <anonymous example.com> writes:
On Tuesday, 15 July 2014 at 14:05:14 UTC, Alexandre wrote:
 Strange..., why ' ' ?
because x"40" == " "
Jul 15 2014