# C++ character question



## GSquadron (Sep 14, 2011)

In order not to make this thread dump, i thought to throw a weird question.
Whenever i try to put for example this code:

```
#include <iostream>

using namespace std;

int main()
{
    char hex[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','f'};
    for(char a; a <= 100; a++){
        cout << hex[a];
    }
}
```

I get weird symbols if 'a' is for example 100. Are these part of the memory populated in the hard drive?

Warning: Do not try to put it 1000 cuz i don't really know what would happen!


----------



## temp02 (Sep 14, 2011)

Your array only has 16 indexes (0 -> 15), on more complex situations you would get an "array index out of bounds / access violation at memory address 0xX" error if you try to access an index outside of this scale (eg: 100). In this case, mostly due to the C compiler, you get whatever is at the next memory position of memory after the array (random data).



Aleksander Dishnica said:


> Are these part of the memory populated in the hard drive?



? (seriously this one made me go "waht da...")


----------



## GSquadron (Sep 14, 2011)

I said that, because there are spaces than come symbols, again spaces, than again symbols....
Actually, try putting 1000000 on the a, if you are sure that has nothing to do with your pc xD
Ok, so i get whatever is next of the memory position, so that memory is populated. (the population in the hard drive)
If, i am wrong here, pls clarify me


----------



## temp02 (Sep 14, 2011)

The data that you get from and index out of the array bounds is not relevant, sure sometimes it can be similar or even the same but what you must understand is that what you are doing is wrong, no program should access, be it to read or write, memory that is not allocated by itself, it will only create problems later on when your application code gets bigger and more complex (if this is for school get ready for a lecture from your teacher  )

As for the "memory on HDD" part, you might be thinking about the pagefile (am I right?).
All the programs are oblivious to if there is a pagefile on the system or not, the management of this is done by the kernel of the OS, and if some memory positions/tables are on the pagefile when a program "asks" to access it, the are moved to the RAM prior to it, once again by the kernel, so having a pagefile on or off is the same for all the applications, you just get more (virtual) RAM by using it. So no you can't access data that is on the HDD this way 

Also, if you are trying to convert text/integers to an hexadecimal string representation I could explain/write a simple algorithm for you .


----------



## ron732 (Sep 14, 2011)

There should be a switch in your compiler that would enforce bounds checking and flag this problem. Like temp02 said the weird characters are random data in your RAM (not the hard drive) that you are displaying. Once you go beyond the array bounds you may get random data because the memory has not been allocated or it may be real data that has been placed in memory by another application.


----------



## GSquadron (Sep 14, 2011)

@temp02
In fact this is not for school and actually all of my questions are not for school 
I am just trying to learn as much c++ as possible (I luv it), so if you want to show me any code
and comments (cuz there must be something i can't understand of course)
I would appreciate that dear teacher 

Also, I got the meaning of the weird symbols, but i was really thinking they come from the hdd!


----------



## temp02 (Sep 14, 2011)

Here is a very very simple example of how to convert a number into an hexadecimal string representation:

```
[COLOR="SeaGreen"]#include <cstdlib>
#include <iostream>[/COLOR]

[B]using namespace[/B] std;

[B]int [/B]main([B]int [/B]argc, [B]char [/B]*argv[])
{    
    [B]int [/B]number = 69; [COLOR="RoyalBlue"]// number to convert, you can put anything here, negative numbers will probably lead to "unexpected" results though (they are still right though)[/COLOR]
    
    [B]char [/B]nibble2hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; [COLOR="RoyalBlue"]// hexadecimal string representation of a [url=http://en.wikipedia.org/wiki/Nibble]nibble[/url][/COLOR]
    [B]char [/B]result[8]; [COLOR="RoyalBlue"]// the resulting string (since 32bits = 4 bytes, each byte = 2 [url=http://en.wikipedia.org/wiki/Nibble]nibble[/url]s, then 8 chars)[/COLOR]
    
    [COLOR="RoyalBlue"]// for all the [url=http://en.wikipedia.org/wiki/Nibble]nibble[/url]s in the 32bits number (4bits = 1 [url=http://en.wikipedia.org/wiki/Nibble]nibble[/url], 8 [url=http://en.wikipedia.org/wiki/Nibble]nibble[/url]s)[/COLOR]
    [B]for [/B]([B]int [/B]i = 0; i < 8; i++)
    {
        [B]int [/B]nibble = number [url=http://en.wikipedia.org/wiki/Bitwise_operation#Logical_shift]>>[/url] (i * 4); [COLOR="RoyalBlue"]// remove the bits already processed (4bits = 1 nibble), by shifting them right couple of positions, dependeng on the nibble being processed[/COLOR]
        nibble = nibble [url=http://en.wikipedia.org/wiki/Bitwise_operation#AND]&[/url] 0xF; [COLOR="RoyalBlue"]// remove the bits not processed yet, the other most significant bits (left ones) get "removed" by performing a logical AND operation (X & 0 = 0, X & 1 = X)[/COLOR]
        
        [B]char [/B]hex_char = nibble2hex[nibble]; [COLOR="RoyalBlue"]// get the hex representation for this nibble[/COLOR]
        
        [COLOR="RoyalBlue"]// now place the curent hex_char on the correct position of the result string...
        // ... since we are calculationg things from the least significant bit up to the most significant bit we must fill the string from right to left too[/COLOR]
        [B]int [/B]position = 8 - i; [COLOR="RoyalBlue"]// calculate the string position from the current bit[/COLOR]
        result[position - 1] = hex_char; [COLOR="RoyalBlue"]// put the char in the string[/COLOR]
    }
    
    [COLOR="RoyalBlue"]// now we need to add an extra padding to the string, the famous string terminator [url=http://en.wikipedia.org/wiki/%5C0]"\0"[/url][/COLOR]
    [B]char [/B]correct_string[9];
    memcpy(correct_string, result, 8);
    correct_string[8] = '\0'; 
    
    [COLOR="RoyalBlue"]// print the sucker :)[/COLOR]
    cout << [COLOR="Red"]"0x"[/COLOR] << correct_string << endl;
    
    system([COLOR="Red"]"PAUSE"[/COLOR]);
    [B]return [/B]EXIT_SUCCESS;
}
```

As I said this was coded to be very basic/simple on purpose so you understand at least the point of the conversion.
In a nutshell: extract each set of 4bits, a nibble, from the number, convert this nibble into one char and append this char to the resulting string, repeat until done.
I added some links to the major operations so you can check for more info other than the one on the comments (also added some minor color highlight to make it more readable on the forum ).
Have fun


----------

