Jump to content

Hex to Decimal of AutoIt is Different with C#


Recommended Posts

Hi AutoIt programmers, excuse me for bothering you with multiple topics.

In AutoIt we can use Number() function to convert Hex string to number but it's output is different of C# output & and i wanna make it's output like AutoIt code.

For e.g I use this in AutoIt:

Local $dBinary = Binary("Hello") ; Create binary data from a string.
Local $dExtract = Number(BinaryMid($dBinary, 1, 5))
ConsoleWrite($dExtract & @CRLF)

And i use this for C#:

using System;
using System.Text;

//NameSpace Is Use of Project Name 
namespace TEST
    class Program
        public static void Main(string[] args)
            //declaring a variable and assigning hex value
            string dd = ToHex("Hello", Encoding.ASCII);
            decimal d = Int64.Parse(dd, System.Globalization.NumberStyles.HexNumber);

            Console.WriteLine("Result: " + d);
            //hit ENTER to exit

        public static string ToHex(string sInput, Encoding oEncoding, bool b0x_Prefix = false)
            byte[] a_binaryOutput = oEncoding.GetBytes(sInput);
            string sOutput = BitConverter.ToString(a_binaryOutput).Replace("-", "");

            if (b0x_Prefix == false)
                return sOutput;
                return "0x" + sOutput;

I say once again that excuse me for creating new topic, in fact i'm making a library for GAuthOTP from a topic in AutoIt.

Edited by Colduction
Aded little more detail
Link to comment
Share on other sites

  • Moderators


I believe you should be using Dec and not Number:

$dBinary = Binary("Hello")
ConsoleWrite($dBinary & @CRLF)

$dExtract_1 = Number(BinaryMid($dBinary, 1, 5))
ConsoleWrite($dExtract_1 & @CRLF)

$dExtract_2 = Dec(StringTrimLeft($dBinary, 2))
ConsoleWrite($dExtract_2 & @CRLF)

ConsoleWrite("0x" & Hex($dExtract_1, 10) & @CRLF)
ConsoleWrite("0x" & Hex($dExtract_2, 10) & @CRLF)

As you can see, using Dec allows you to recreate the original string:


Does the Dec value match the C# output?


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


Link to comment
Share on other sites

1 hour ago, Melba23 said:

$dExtract_1 = Number(BinaryMid($dBinary, 1, 5)) ConsoleWrite($dExtract_1 & @CRLF)

@Melba23 this line is what i want to tranform to C# code, but the out put of C# code is different (I've mentioned the C# code above)

In fact, i'm transforming:

to C#, because i think it's more optimized than other libs in Nuget, anyway i wanna do excersice :D

Link to comment
Share on other sites

  • Colduction changed the title to Hex to Decimal of AutoIt is Different with C#

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...