• (cs) in reply to AC
    Anonymous:
    Anonymous:
    JohnO:
    <FONT size=2>

    System.Diagnostics.Debug.WriteLine(Int32.Parse("A", System.Globalization.NumberStyles.HexNumber).ToString());

    </FONT>


    This is a joke, right?

    no, this is .NET

    What's the difference?

     

    Sorry, couldn't resist.

  • David (unregistered) in reply to dubwai
    dubwai:
    Anonymous:
    Anonymous:
    JohnO:
    <FONT size=2>

    System.Diagnostics.Debug.WriteLine(Int32.Parse("A", System.Globalization.NumberStyles.HexNumber).ToString());

    </FONT>


    This is a joke, right?

    no, this is .NET

    What's the difference?

     

    Yeah, seriously.  Real men code in VB6! 

    *runs*

  • (cs) in reply to res2
    res2:
    Anonymous:
    Anonymous:
    JohnO:
    <font size="2"> </font>

    <font size="2">System.Diagnostics.Debug.WriteLine(Int32.Parse("A", System.Globalization.NumberStyles.HexNumber).ToString());</font>


    This is a joke, right?

    Nice setup.  When someone says "It's not a joke, it's printing to the screen," will you start ranting about how much you think C# sucks, or will you smugly show a way to do it with 50% less code?

    Man, the suspense is killing me.

    Me too, pass the popcorn please :)



    Long names for identifiers or depth of classes does not directly correspond to "more code".  It's still a single statement, which is fine with me.  Smalltalk, Java and Delphi programmers have dealt with this for years, new C# coders coming from a procedureal environment (Pascal, C, REXX, etc) think it's abhorrent to have such "busy" names for identifiers.

    All I can say is... take a gander at any C or C++ header file for Windows and tell me the identifers for some of those constants don't throw you for a loop!
  • (cs) in reply to dan
    Anonymous:
    wierd it cut me off.

    I think the joke is "System.Diagnostics.Debug.WriteLine" is a lot longer than "out.write()"

    and System.Globalization.NumberStyles.HexNumber is a lot longer than str2hex()



    See, there's this fabulous thing called a using clause that lets you abbreviate namespace complexity.

    Consider System.Console.Writeline.

    You could do this:

    using System;
    using out = System.Console;

    Now, you can do this:

    out.WriteLine("foo");

    Better yet, since you have assigned an identifier of "out" to System.Console, you can easily switch out System.Console to something else.  I have done this very thing.

    I wrote a static class called SocketReporter which is a multithreaded tcp server that resembles the console.  As the app runs, all output going to out is sent to any tcp clients which are connected to the SocketReporter service when it's running.  This makes it a breeze to debug Win32 service apps.  I can run the .exe in the debugger and have it write to the system console when necessary, then swap over to SocketReporter with a conditional compile for Release mode when I'm running the service on a server and monitor its output.

  • (cs) in reply to christoofar

    christoofar:
    Anonymous:
    wierd it cut me off.

    I think the joke is "System.Diagnostics.Debug.WriteLine" is a lot longer than "out.write()"
    and System.Globalization.NumberStyles.HexNumber is a lot longer than str2hex()


    See, there's this fabulous thing called a using clause that lets you abbreviate namespace complexity.

    Consider System.Console.Writeline.

    You could do this:

    using System;
    using out = System.Console;

    Couldn't you just declare a variable and point it to System.Console?

  • (cs) in reply to heinzkunz

    Note that the Debug.WriteLine is only necessary if you want to print to the debug window, and could be replaced by either MessageBox.Show() or Console.WriteLine() (depending on whether it's a console or windows forms app), which would be a fairer comparison.  You can also shorten it down quite a lot, either by using the "using" keyword to specify the namespace at the top of your source file, or by using a different choice of methods.

    Oh, and your Java code wouldn't actually compile.  You're missing a ')'.

    How about this:

    System.Windows.Forms.MessageBox.Show(Convert.ToInt32("ABC", 16));

    which, if you have System.Windows.Forms included in your imported classes at the top of your .cs file, can become this:

    MessageBox.Show(Convert.ToInt32("ABC", 16));

    The actual conversion is the Convert.ToInt32() method.  "But sir, I don't know how to read MSDN!  Waaa!"  http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemConvertClassToInt32Topic19.asp

    On a slightly more controversial note, are we now judging languages solely on how many characters I need to type?  I'll stick with C#, since it doesn't have classes infested with horrible Hungarian notation (unlike C++), and the IDE takes most of the work out of it anyway with Intellisense.

  • (cs) in reply to dubwai

    Darn will I ever get used to the way this forum works - I always forget I'm only on the first page.  Anyway...

    Couldn't you just declare a variable and point it to System.Console?

    Er, isn't that what he did?

  • (cs) in reply to johnl
    johnl:
    Oh, and your Java code wouldn't actually compile.  You're missing a ')'.

    Yeah, relying on your compiler to tell you when you have compile errors 'encourages laziness'
  • (cs) in reply to Mung Kee

    LOL.  I was just nitpicking, I know.

  • James (unregistered) in reply to dhromed

    dhromed:
    James:
    I hate to say it, but your post has exposed a pretty fundamental flaw in your understanding of the way the Javascript language actually works.


    I don't claim to understand how any language works at the core. :)

    I'm one of those self-taught idiots. I'm not formally schooled in anything related to computer science and/or programming.

    I'm self taught too.

    The point I was making was that setting a variable equal to a number means that the variable contains just that - a number.  It doesn't matter what syntax you use to set the value; the end result's the same.

    Therefore, when you print it out again, of course it's going to print out as a decimal!  It's not a bug and it's not a special feature.  It's basic behaviour of the language.

    <FONT face="Lucida Console" size=2>  var hex1 = 0x0f;</FONT>

    <FONT face="Lucida Console" size=2>does exactly the same thing as:</FONT>

    <FONT face="Lucida Console" size=2>  var hex1 = 15;</FONT>

    <FONT face="Lucida Console" size=2>They both set the variable hex1 equal to the number 15.</FONT>

    So when you print out the contents of hex1, you're going to get 15.

  • heinzkunz (unregistered) in reply to johnl

    John,

    why do some coders become touchy if someone says something that might possibly shed a bad light onto their language? No language is 'perfect'.

    I have replied to the question: "will you smugly show a way to do it with 50% less code?" No more, no less.

    I am not interested in something as useless as language bashing.

  • (cs) in reply to johnl

    "I'll stick with C#, since it doesn't have classes infested with horrible Hungarian notation (unlike C++)"

    I'm sorry - where in the standard C++ language is there any hungarian notation? In fact, the guys behind C++ are pretty much against hungarian notation, full stop.

    Now some Microsoft libraries may have hungarian in them, but that's Microsoft. And they may have finally learnt their lesson if, as you imply, C# doesn't have it in its standard libraries.

  • (cs) in reply to James
    Anonymous:

    dhromed:
    James:
    I hate to say it, but your post has exposed a pretty fundamental flaw in your understanding of the way the Javascript language actually works.


    I don't claim to understand how any language works at the core. :)

    I'm one of those self-taught idiots. I'm not formally schooled in anything related to computer science and/or programming.

    I'm self taught too.

    The point I was making was that setting a variable equal to a number means that the variable contains just that - a number.  It doesn't matter what syntax you use to set the value; the end result's the same.

    Therefore, when you print it out again, of course it's going to print out as a decimal!  It's not a bug and it's not a special feature.  It's basic behaviour of the language.

    <font face="Lucida Console" size="2">  var hex1 = 0x0f;</font>

    <font face="Lucida Console" size="2">does exactly the same thing as:</font>

    <font face="Lucida Console" size="2">  var hex1 = 15;</font>

    <font face="Lucida Console" size="2">They both set the variable hex1 equal to the number 15.</font>

    So when you print out the contents of hex1, you're going to get 15.



    I was somehow under the impression that a number var would retain the format it was defined in.

    So it doesn't.

    I can sleep again.
  • (cs) in reply to heinzkunz

    Anonymous:
    John, why do some coders become touchy if someone says something that might possibly shed a bad light onto their language? No language is 'perfect'. I have replied to the question: "will you smugly show a way to do it with 50% less code?" No more, no less. I am not interested in something as useless as language bashing.

    Sorry, I got a bit carried away.  I just wanted to point out that it wasn't a fair comparison.  But the point stands - the fact that JavaScript can do it in fewer characters doesn't make JavaScript a better language.  If you weren't trying to make that comparison, I can't see wat point you were trying to make.

    About hungarian notation - it's not just the standard library, there's also all the 3rd-party libraries, such as the VCL, STL or MFC.  All these IIRC favour hungarian notation in some form, or like to use other forms of cryptic abbreviations for their member names.

  • (cs) in reply to johnl

    VCL I don't know, and a google for it gives, as a first hit, a library of 'furry' art which I dare not click. MFC is MS, so doesn't count as 'third party'

    STL may have some oddish naming conventions internally, but externally it's not hungarian..

  • (cs) in reply to tufty

    VCL is Borland's Visual Component Library.  Basically the STL for graphical controls.  The STL I seem to remember having lots of strange names - didn't they have T (for Type) as a prefix to most class names?.  MFC I count as 3rd party, unless you're saying it's standard C++.  When "standard libray" was mentioned earlier, I assumed it was talking about standard C++ as defined by Bjarne Stroustrup, which MFC is not.

  • (cs) in reply to johnl

    VCL is Borland's Visual Component Library

    Ah, right. I know nothing of it.

    The STL I seem to remember having lots of strange names

    • didn't they have T (for Type) as a prefix to most class names?

    Nope. That would be mfc or root or something like that. STL is nice clean 'vector<foo>' and the suchlike. There is '_Tp', which is a pointer to a type, but I'm fairly sure it's only used internally. STL is actually very readable and usable once you get the hang of it, but these days I don't torture myself with C++ unless I absolutely have to :)

    RogueWave was all 'Collectable' and so on, IIRC, can't remember any hungarian there either. It was still horrible to use, though, and a monstrous pain if you upgraded versions.

    It's been a while though.

    When "standard libray" was mentioned earlier, I assumed it was talking about standard C++ as defined by Bjarne Stroustrup, which MFC is not.

    Indeed. What I posted was somewhat misleading, I was trying to make the distinction between 'standard', '3rd party' and 'Microsoft'. My bad. I was discounting MFC due to its very microsoftness.

    In summary, I'd say that, yes, there are 3rd party non-MS class libraries that use hungarian out there, but probably not the majority.

    Simon

  • (cs) in reply to christoofar

    christoofar:

    See, there's this fabulous thing called a using clause that lets you abbreviate namespace complexity.

    <FONT face="Courier New" size=2>s/abbreviate/abuse</FONT>

  • (cs) in reply to emptyset

    Microsoft Soviet-Era C++ programming:

    BOOL CVeryBigClass::GetResult()
    {
    BOOL bBoolResult = (
    GetScaryApiInfoEx(
    m_hHandle,
    m_nNumber,
    m_lpszString,
    NULL,
    NULL,
    NULL,
    NULL,
    // Many more lines of NULLs
    // omitted for brevity
    NULL)) ? TRUE : FALSE;
    if (bBoolResult == TRUE)
    return TRUE;
    else if (bBoolResult != TRUE)
    return FALSE;
    else
    return FALSE;
    }

  • (cs)

    Well, at least this code can easily be changed to add very large hex numbers, much bigger than the largest value of <b>int</b>. That's something you can't do with Int.Parse(). It is obviously superfluous for 8 digit hex numbers; maybe the code was taken from a library that handles larger numbers, e.g. for calculating RSA keys. BTW: Building a solution with Int32.Parse()+Int32.Parse() would be faster, but give you wrong results when you have an integer overflow. To savely add two 8-digit hex numbers, you need at least 33 bit.

  • (cs) in reply to tufty

    @tufty:  We are talking about the same STL, right?  As in Borland's Standard Template Library (IIRC, and it has been a while, Borland did pretty much the same thing as MFC - a component library, but split into visual and non-visual components).  Guess it must just be the VCL and whatever they called their non-visual library (which I thought was called the STL but I guess not).  I know that they have a lot of stuff like TData, TQuery and so on.

    My bad. I was discounting MFC due to its very microsoftness.

    No worries

    In summary, I'd say that, yes, there are 3rd party non-MS class libraries that use hungarian out there, but probably not the majority.

    I dunno, both MS and Borland do it (though MS has indeed strated to ditch this strategy, if only for cross-language compatibility and cleanness in .NET) and most people take their cue from them.  Certainly, the software (written in Delphi with a bit of C++) we produce at work uses it extensively, and it was what I was always taught to do.  It might be different in the non-Windows world, though.

  • (cs) in reply to johnl

    @tufty: We are talking about the same STL, right? As in Borland's Standard Template Library

    No, then. Most certainly not. If Borland have called their abortion 'STL', they are asshats of a fairly major calibre.

    http://www.sgi.com/tech/stl/ is the STL

    :)

  • (cs) in reply to dhromed
    dhromed:

    I was somehow under the impression that a number var would retain the format it was defined in.


    So it doesn't.



    A number is an abstract concept and does NOT have a base. A base only comes into play when your want to represent a number physically as a sequence of digits. Internally, all numbers are in fact stored in binary. Other bases only become relevant when there is a conversion between the internal format and a string. That's what happens to the number definition in the source code: the compiler's or interpreter's parser turns the string "<font face="Lucida Console" size="2">0x0f</font>" into the internal binary representation, using base 16 because it starts with 0x.. When you want to turn it back into a string, that's when the base again becomes relevant, the default being 10.

  • (cs)

    It isn't clear if the sign of the hex strings is to be treated like VB normally treats them.  It appears that the two hex strings are added as unsigned (positive) values. 

    Unfortunately, VB only supports hex literals and operations that can be represented as a long integer.  Long Integers are always signed.

    There are at least three datatypes with enough bits to accomodate the largest possible value of the sum of two 8 hex digit strings (Double, Currency, Decimal).  The problem with this approach comes in when trying to convert the sum back to hex characters (see prior point).

    (snicker)  <FONT face="Courier New">"= Right(h3,16)"</FONT>

    I wonder if this code wasn't originally part of Excel VBA, with the <FONT face="Courier New">hex2dec()</FONT> function reference. ;-)

    With no specific datatype declaratives, I assume that either this code is run in a scripting environment (ASP, VBScript) or performance has never been a concern of the programmer or the IT staff usually concerned with such things.

    ==========================

    For brevity and efficiency, and sticking within the VB language, I would suggest the following solution.

    1. convert both parameter strings into fixed-length 8 hex character strings.
    2. add the numeric values represented by the 6 right most hex digits of the two parameters.  We will output the right 6 digits and use the left two digits as the carry data into step3.
    3. add the two left-most hex characters of the parameters and the left-most two characters from the step2 addition.
    4. concatenate four step3 digits and 6 step2 characters
    5. output a fixed-length 10 hex digit sum

    <FONT color=#000000>My implementation example:</FONT>

    <FONT face="Courier New">Function AddHex(hex1, hex2)
        'Adds two hexadecimal strings (up to 8-digits each)</FONT>

    <FONT face="Courier New">    Dim h1, h2, hRight6, Carry, hLeft4
        'Step1 - convert to fixed length (8) strings
        h1 = Right("00000000" & hex1, 8)
        h2 = Right("00000000" & hex2, 8)
       
        'Step2 - add right most 6 hex digits
        hRight6 = Right("00000000" & Hex(CLng("&h" & Right(h1, 6)) + CLng("&h" & Right(h2, 6))), 8)
        Carry = Left(hRight6, 2)
        hRight6 = Right(hRight6, 6)
       
        'Step3 - add the high-order digits and the carry (if any)
        hLeft4 = Hex(CLng("&h" & Left(h1, 2)) + CLng("&h" & Left(h2, 2)) + CLng("&h" & Carry))
       
        'Step4 and Step5 - concatenate the hex strings and make fixed length (20)
        AddHex = Right("0000000000" & hLeft4 & hRight6, 10)
    End Function</FONT>
    =========================

    If this needed to be extended to larger hex strings, I would probably use three numeric arrays, with each position holding the value of pairs of hex digits.  The third array would be used for carry data.  Output would consist of individual array item conversion from numeric value to hexidecimal string equivalence using the <FONT face="Courier New">Right("0" & Hex(),2)</FONT> function combinations as shown above.

  • (cs) in reply to johnl
    johnl:

    About hungarian notation - it's not just the standard library, there's also all the 3rd-party libraries, such as the VCL, STL or MFC.  All these IIRC favour hungarian notation in some form, or like to use other forms of cryptic abbreviations for their member names.



    Ah, VCL, STL and MFC. Third party libraries. One of these things is not like the others.

    You WERE making some sort of obscure joke, right? Right?

    *cries*
  • (cs) in reply to rsynnott

    Read the exchanges between me and tufty.

    I remembered where I got that idea from, I think.  It was a college tutor.  We were doing basic C++, and I asked if I should read about the STL.  He said no, that was just a bunch of libraries written by Borland, similar to MFC, and the knowledge would be useless if you weren't using C++ Builder.  I left that course soon after because they were cutting off the programming side in the second year (maybe that was why).  That was the last time (apart from an abortive attempt at a game with some other guys) that I looked at C++.  I decided I preferred C#.

  • (cs) in reply to hmm

    In VB, the "&H" is the hex identifier. So whereas you would write something like this in C#:

    int i = 0xFA;

    You would write this in VB.NET:

    dim i as integer = &HFA

    Or this in VB6:

    dim i as integer
    i = &HFA

    As for C#'s conversion of hex strings:

    string hexValue = "FA";
    int i = Convert.ToInt32(hexValue,16);   // second parameter indicates the base to convert from

  • (cs) in reply to limelight

    Sorry ... I'm new to the board and still not sure how to include the original message in my reply. The above message was meant as a reply to the message that said:

    "Ok I'm not familiar with VB at all...what does "&H" accomplish?  And how would this be accomplished in a language like C#? Because I'm not aware of any way to convert a hex string to int or long in that language.  Int32.Parse( string ) will throw an exception if you give it something like "F" or "0xF"..."

Leave a comment on “Hexing Around”

Log In or post as a guest

Replying to comment #:

« Return to Article