# Loop that would use hexadecimal numbers(0123456789ABCDEF)

Page 1 of 1

## 5 Replies - 1084 Views - Last Post: 02 May 2014 - 09:44 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=345895&amp;s=4fd118ef3653d6c433999863d5a7a23b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ro12hit

Reputation: 0
• 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.

Is This A Good Question/Topic? 0

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

### #2 Skydiver

• Code herder

Reputation: 4094
• Posts: 13,070
• 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);

```

### #3 ro12hit

Reputation: 0
• 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.

### #4 Skydiver

• Code herder

Reputation: 4094
• Posts: 13,070
• 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);
}

```

### #5 ro12hit

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

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

Posted 02 May 2014 - 04:18 AM

### #6 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4747
• Posts: 8,450
• 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:

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.