5 Replies - 610 Views - Last Post: 02 May 2014 - 09:44 AM Rate Topic: -----

#1 ro12hit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-April 14

Loop that would use hexadecimal numbers(0123456789ABCDEF)

Posted 28 April 2014 - 09:30 PM

Hi!

I am trying to make a for or foreach loop that would instead of normal integers use hexadecimal numbers (0123456789ABCDEF). What i mean here is after i=9 I need i=A and so on. So I was think of storing these values into a enums and then using a foreach of enum. But I feel that there must be some other easy way to do it. Is using the enum is best approach or can I use any other approach. Please reply me if u have any solution.

Thanks in advance.............

Is This A Good Question/Topic? 0
  • +

Replies To: Loop that would use hexadecimal numbers(0123456789ABCDEF)

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3491
  • View blog
  • Posts: 10,748
  • Joined: 05-May 12

Re: Loop that would use hexadecimal numbers(0123456789ABCDEF)

Posted 28 April 2014 - 09:56 PM

Huh? What exactly are you trying to do? Right now I suspect that you don't understand that hexadecimal numbers are just a different way of showing the same decimal value.

Try running this:
for (int i = 0; i < 16; i++)
    Console.WriteLine("{0:X} is equal to {0:D}", i);


Was This Post Helpful? 2
  • +
  • -

#3 ro12hit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-April 14

Re: Loop that would use hexadecimal numbers(0123456789ABCDEF)

Posted 28 April 2014 - 11:13 PM

I am really sorry I know it was a stupid question. But I do know that hexadecimal numbers are just a different way to show same decimal value. I was not talking about the value but about string itself. I don't know how to say that exactly. But I found solution for my problem and now I feel like an idiot to have asked this question. The solution is like this:
            string a= "123456789ABCDEF";
            foreach (char alpha in a)
            {
                string b = "3" + alpha;                
            }



so here I will get from "31" to "3F". where three will be a constant and the second digit with rotate.
sorry to waste your time........next time I will think better before asking a question.
Anyway thanks for the reply....
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3491
  • View blog
  • Posts: 10,748
  • Joined: 05-May 12

Re: Loop that would use hexadecimal numbers(0123456789ABCDEF)

Posted 29 April 2014 - 05:47 AM

Oh, I see.

Other options:

for(int i = 0x30; i <= 0x3F; i++)
{
    var hex = i.ToString("X");
    Console.WriteLine(hex);
}



foreach(var hex in Enumerable.Range(0x30, 0xF)
                             .Select(n => n.ToString("X")))
{
    Console.WriteLine(hex);
}


Was This Post Helpful? 0
  • +
  • -

#5 ro12hit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-April 14

Re: Loop that would use hexadecimal numbers(0123456789ABCDEF)

Posted 02 May 2014 - 04:18 AM

Thanks for the reply...........
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4440
  • View blog
  • Posts: 7,721
  • Joined: 08-June 10

Re: Loop that would use hexadecimal numbers(0123456789ABCDEF)

Posted 02 May 2014 - 09:44 AM

Just to clarify what Skydiver is showing you there, there's two concepts:

1. Hexadecimal literals
2. ToString formatting

Hexadecimal literals are treated just like any other number. The way you signify that a literal is hex is by prefixing it with 0x. So to represent the number 20 in hex, you'd write it 0x14. Again, this is treated no differently than any other number. I can save that number to an int variable the same as if it were a decimal number.

int i = 0x14; //i has a value of DEC 20 / HEX 14


So you can use your hex numbers as a loop control variable or anything else.

Now the next thing is printing them out. The computer doesn't care about what format the numbers are in, it stores them in a binary format anyway. It translates that to DEC when it converts an int to a string. But by providing a parameter to the .ToString method, you can change what base it outputs the number in. For hex, that particular argument happens to be "X".

So that way, you don't have to deal with manually building a list, you can just use normal numbers and print them as hex.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1