Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
February 03, 2016 How do you get a hexstring from a base10 string -or- from a number? | ||||
---|---|---|---|---|
| ||||
I am making a method called: @property string debugIDString() { in { assert(super.toHash() == this.toHash()); } body { } |
February 03, 2016 Re: How do you get a hexstring from a base10 string -or- from a number? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Enjoys Math | On Wednesday, 3 February 2016 at 23:43:45 UTC, Enjoys Math wrote:
> I am making a method called:
>
> @property string debugIDString() {
> in {
> assert(super.toHash() == this.toHash());
> } body {
>
> }
body { // is currently:
return to!string(this.toHash());
}
and is returning a base10 string, so how would I return a hex string so I can compare numbers displayed to the debugger addresses in visual D?
|
February 03, 2016 Re: How do you get a hexstring from a base10 string -or- from a number? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Enjoys Math | On Wednesday, 3 February 2016 at 23:45:15 UTC, Enjoys Math wrote:
> On Wednesday, 3 February 2016 at 23:43:45 UTC, Enjoys Math wrote:
>> I am making a method called:
>>
>> @property string debugIDString() {
>> in {
>> assert(super.toHash() == this.toHash());
>> } body {
>>
>> }
>
> body { // is currently:
> return to!string(this.toHash());
> }
>
> and is returning a base10 string, so how would I return a hex string so I can compare numbers displayed to the debugger addresses in visual D?
One solution: create "string_tools.d":
module string_tools;
import std.conv: to;
string hexString(int x) {
string hex = "0x";
for(uint k=0; k < 8; k++) {
int hexDig = (x >> (k << 2)) & 0x0000000F;
if (hexDig < 10) {
hex ~= to!string(hexDig);
}
else {
string hexDixStr;
switch (hexDig) {
case 10: hexDigStr = "A"; break;
case 11: hexDigStr = "B"; break;
case 12: hexDigStr = "C"; break;
case 13: hexDigStr = "D"; break;
case 14: hexDigStr = "E"; break;
case 15: hexDigStr = "F"; break;
}
hex ~= hexDigStr;
}
}
return hex;
}
|
February 03, 2016 Re: How do you get a hexstring from a base10 string -or- from a number? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Enjoys Math | On Wed, Feb 03, 2016 at 11:45:15PM +0000, Enjoys Math via Digitalmars-d-learn wrote: > On Wednesday, 3 February 2016 at 23:43:45 UTC, Enjoys Math wrote: > >I am making a method called: > > > >@property string debugIDString() { > >in { > > assert(super.toHash() == this.toHash()); > >} body { > > > >} > > body { // is currently: > return to!string(this.toHash()); > } > > and is returning a base10 string, so how would I return a hex string so I can compare numbers displayed to the debugger addresses in visual D? @property string debugIDString() { import std.format; return format("%x", this.toHash()); } T -- Study gravitation, it's a field with a lot of potential. |
February 04, 2016 Re: How do you get a hexstring from a base10 string -or- from a number? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Enjoys Math | On 04.02.2016 00:45, Enjoys Math wrote:
> On Wednesday, 3 February 2016 at 23:43:45 UTC, Enjoys Math wrote:
> body { // is currently:
> return to!string(this.toHash());
> }
>
> and is returning a base10 string, so how would I return a hex string so
> I can compare numbers displayed to the debugger addresses in visual D?
to!string has an optional radix (aka base) parameter:
return to!string(this.toHash(), 16);
|
Copyright © 1999-2021 by the D Language Foundation