PureBasic Forum
http://forums.purebasic.com/english/

Anyone know C# can help make this to PB
http://forums.purebasic.com/english/viewtopic.php?f=13&t=73053
Page 1 of 1

Author:  vwidmer [ Thu Jun 20, 2019 2:05 am ]
Post subject:  Anyone know C# can help make this to PB

Any one know C# can help put this to PB thanks

Code:
public static string GenerateDeviceToken()
{
    List<int> rands = new List<int>();
    var rng = new Random();
    for (int i = 0; i < 16; i++)
    {
        var r = rng.NextDouble();
        double rand = 4294967296.0 * r;
        rands.Add(((int)((uint)rand >> ((3 & i) << 3))) & 255);
    }

    List<string> hex = new List<string>();
    for (int i = 0; i < 256; ++i)
    {
        hex.Add(Convert.ToString(i + 256, 16).Substring(1));
    }

    string id = "";
    for (int i = 0; i < 16; i++)
    {
        id += hex[rands[i]];

        if (i == 3 || i == 5 || i == 7 || i == 9)
        {
            id += "-";
        }
    }

    return id;
}


Sorry I dont fully understand it.
thanks

Author:  Demivec [ Thu Jun 20, 2019 3:10 am ]
Post subject:  Re: Anyone know C# can help make this to PB

I don't use C# but when I put the original in the blender this is what came out :) :
Code:
Procedure.s GenerateDeviceToken()
  Protected Dim rands.q(15)
  Protected Dim hexValue.s(255)
  Protected id.s = "", i, r.d, rand.d
 
  ;create 15 random numbers in range $00 to $FF
  For i = 0 To 15
    r = (Random(2147483647) * 2147483647 + Random(2147483647)) / 4611686016279904256 ;generate random double 0.0 to 1.0
    rand = 4294967296.0 * r
    rands(i) = ( Int(rand) >> ((3 & i) << 3) ) & 255
  Next
 
  ;create hex string table for conversion
  For i = 0 To 255
    hexValue(i) = Right(Hex(i + 256), 2)
  Next
 
  ;build the token string
  For i = 0 To 15
    id + hexValue(rands(i))
    If (i = 3 Or i = 5 Or i = 7 Or i = 9)
      id + "-"
    EndIf
  Next
 
  ProcedureReturn id
EndProcedure


;test
Define x
For x = 1 To 1000
  Debug GenerateDeviceToken() ;displays values such as E3CDBEFD-F9C2-68F5-0F12-8B01C2CA86F7"
Next


It can easily be shortened to this:
Code:
Procedure.s GenerateDeviceToken()
  Protected Dim rands.q(15)
  Protected id.s = "", i, r.d, rand.d
 
  ;create 15 random numbers in range $00 to $FF
  For i = 0 To 15
    r = (Random(2147483647) * 2147483647 + Random(2147483647)) / 4611686016279904256 ;generate random double 0.0 to 1.0
    rand = 4294967296.0 * r
    rands(i) = ( Int(rand) >> ((3 & i) << 3) ) & 255
  Next
 
  ;build the token string
  For i = 0 To 15
    id + Right(Hex(rands(i) + 256), 2)
    If (i = 3 Or i = 5 Or i = 7 Or i = 9)
      id + "-"
    EndIf
  Next
 
  ProcedureReturn id
EndProcedure


;test
Define x
For x = 1 To 1000
  Debug GenerateDeviceToken() ;displays values such as E3CDBEFD-F9C2-68F5-0F12-8B01C2CA86F7"
Next


@Edit: For completeness, the solutions have been reworked to function equally well on both 64-bit and 32-bit compilations to generate the desired level of randomness. Thank you Paul for alerting me to these issues dealing with Random() and the max values related to the Integer size in a compilation.

Author:  Paul [ Thu Jun 20, 2019 6:09 am ]
Post subject:  Re: Anyone know C# can help make this to PB

Your first code snippet will crash on x86 at this line...
r.d = Random(9223372036854775807) / 9223372036854775807

Help file says Random(Max) ... Max: This value needs to be positive (zero included) and may not exceed the maximum positive integer value.


Second code snippet on x86 will only return
00000000-0000-0000-0000-000000000000

Author:  Demivec [ Thu Jun 20, 2019 6:39 am ]
Post subject:  Re: Anyone know C# can help make this to PB

Thanks for pointing that out.

Curse these 64 bit eyes of mine. It will take a moment to figure a good approach to this for 32-bit while being true to the codes intended purpose.

Author:  BarryG [ Thu Jun 20, 2019 6:54 am ]
Post subject:  Re: Anyone know C# can help make this to PB

My version:

Code:
Procedure.s GenerateDeviceToken()
  For n=1 To 32
    If Random(1)=0
      t$+Chr(Random(57,48)) ; 0-9
    Else
      t$+Chr(Random(70,65)) ; A-F
    EndIf
  Next
  ProcedureReturn Left(t$,8)+"-"+Mid(t$,10,4)+"-"+Mid(t$,15,4)+"-"+Mid(t$,20,4)+"-"+Right(t$,12)
EndProcedure

For n=1 To 10
  Debug GenerateDeviceToken()
Next

Author:  vwidmer [ Thu Jun 20, 2019 12:06 pm ]
Post subject:  Re: Anyone know C# can help make this to PB

@Demivec @BarryG Thank you very much..

I see it isnt actually doing anything special then just making a random number. Thanks for the help.

Author:  infratec [ Thu Jun 20, 2019 12:17 pm ]
Post subject:  Re: Anyone know C# can help make this to PB

Or the Windows API way:
Code:
Procedure.s GenerateDeviceToken()
 
  Protected *UUID, UUID.GUID, Result$
 
  UuidCreate_(@UUID)
  UuidToString_(@UUID, @*UUID)
  Result$ = PeekS(*UUID)
  LocalFree_(*UUID)
 
  ProcedureReturn Result$
 
EndProcedure

For n=1 To 10
  Debug GenerateDeviceToken()
Next

Author:  vwidmer [ Thu Jun 20, 2019 4:24 pm ]
Post subject:  Re: Anyone know C# can help make this to PB

Is this just a standard UUID or same type of code as UUID??

Author:  infratec [ Thu Jun 20, 2019 5:50 pm ]
Post subject:  Re: Anyone know C# can help make this to PB

It is a 'special' version, which avoids conflicts with already UUIDs in use of the used PC:

https://docs.microsoft.com/en-us/window ... uuidcreate

And ... it geerates UUIDs like all other tools which generates UUIDs :mrgreen:

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/