howtothings.co.uk
Convert Whole numbers to Hex - Printable Version

+- howtothings.co.uk (https://www.howtothings.co.uk)
+-- Forum: Computing (https://www.howtothings.co.uk/forumdisplay.php?fid=4)
+--- Forum: Programming Support, Graphic Design and Tutorials (https://www.howtothings.co.uk/forumdisplay.php?fid=14)
+--- Thread: Convert Whole numbers to Hex (/showthread.php?tid=699)



Convert Whole numbers to Hex - Drumm - 03-01-2011

This code simply takes in a number from command line input (compiled.exe 12345) and converts to hex, doesn't work with decimals, but could easily be modified too, you would need to use an IF statement instead of a case switch.

[code=cpp]#include <iostream>
using namespace std;
#include <math.h>
#include <stdlib.h>


main(int argc, char *argv[]){
int input = atoi(argv[1]);
int len = ceil(log10(input));
int dec[len];
float remainder;
float floa = input;
int x=len;
float fullnumber;

for(;x >= 0; x--){
remainder = floa/16;
cout << floa << " / 16 = " << remainder;
floa = (int)remainder;
remainder = (remainder-(int)remainder)*16; //remainder should be in hex
cout << remainder << endl;
dec[x] = remainder;
}

x = 1;

for(;x <= len; x++){
//cout << (int)dec[x] << endl;
switch( (int)dec[x] ){
case 10:
cout << "a";
break;
case 11:
cout << "b";
break;
case 12:
cout << "c";
break;
case 13:
cout << "d";
break;
case 14:
cout << "e";
break;
case 15:
cout << "f";
break;
default:
cout << dec[x];
break;
}
}
return 0;
}[/code]


RE: Convert Whole numbers to Hex - bigsharn - 03-01-2011

And for those who are interested in seeing it in VB6:

Code:
Option Explicit
Private Sub Form_main()

Dim int as integer
Dim hex as string

*magic happens here*

End sub

I got a bit stuck there


RE: Convert Whole numbers to Hex - Drumm - 03-01-2011

It is possible to do this with some kind of library, but I wanted too challenge myself to learn how to convert Decimal to Hex in my mind.
It's rather simple tbh, I'll explain if somebody asks.


RE: Convert Whole numbers to Hex - latch - 04-01-2011

(03-01-2011, 09:24 PM)Drumm Wrote: It is possible to do this with some kind of library, but I wanted too challenge myself to learn how to convert Decimal to Hex in my mind.
It's rather simple tbh, I'll explain if somebody asks.

I'm impressed that you even care about hex.

I only code once or twice a month(my environment is not conducive to concentration) and read hex about that often. Even programmers don't use hex much anymore as far as I know. High level languages don't get that close to the machine.


RE: Convert Whole numbers to Hex - Pack3t SynAck3r - 04-01-2011

(04-01-2011, 01:00 AM)latch Wrote:
(03-01-2011, 09:24 PM)Drumm Wrote: It is possible to do this with some kind of library, but I wanted too challenge myself to learn how to convert Decimal to Hex in my mind.
It's rather simple tbh, I'll explain if somebody asks.

I'm impressed that you even care about hex.

I only code once or twice a month(my environment is not conducive to concentration) and read hex about that often. Even programmers don't use hex much anymore as far as I know. High level languages don't get that close to the machine.

Hex(base16) is still widely used, but mainly because it is a hell of a lot easier to write 0x9B6D than to write the binary(base2) equivalent of 1001101101101101. It is used generally in "lower" level languages such as C/C++ and ASM, although almost all languages allow for it. The programs' purpose will determine whether or not the inclusion of hex is necessary.
For example the program I have been working on recently steps through 512 bytes of raw data. The code I wrote is 90% operating on different hex values with regard to their position, value and significance. The 512 bytes if looked at as a whole would be called the MBR(master boot record) of a Windows NTFS volume. The program specifically analyzes bootkits (MBR rootkits) and in the short term allows for reverse engineering and disinfection. In the long run it is merely a piece of my master suite of software I have been working on since I was your age.

Although all numbers can be displayed in whatever base form you would like the relationship between hex and binary is more relevant than hex and decimal(base10.) If you are debugging a program you are generally going to need to be able to reference the memory address space your program is using. This is shown in hex. If you ever want to be able to find a flaw in the new ios you will have to understand how to reverse engineer it. This will leave you with a shit load of lines of assembly code with operators on hexadecimal values representing memory space and the processor register. If you ever hope to write a buffer overflow it is an exercise of pushing more data in a space of memory than the program is designed to handle in certain strategic places. It is necessary to write a program to find these strategic points, and to find out what opportunities these strategic points in turn offer you the ability to do. You will certainly be manipulating hexadecimal values if you ever get this far down the road.



RE: Convert Whole numbers to Hex - latch - 04-01-2011

(04-01-2011, 08:59 AM)Pack3t SynAck3r Wrote:
(04-01-2011, 01:00 AM)latch Wrote:
(03-01-2011, 09:24 PM)Drumm Wrote: It is possible to do this with some kind of library, but I wanted too challenge myself to learn how to convert Decimal to Hex in my mind.
It's rather simple tbh, I'll explain if somebody asks.

I'm impressed that you even care about hex.

I only code once or twice a month(my environment is not conducive to concentration) and read hex about that often. Even programmers don't use hex much anymore as far as I know. High level languages don't get that close to the machine.

Hex(base16) is still widely used, but mainly because it is a hell of a lot easier to write 0x9B6D than to write the binary(base2) equivalent of 1001101101101101. It is used generally in "lower" level languages such as C/C++ and ASM, although almost all languages allow for it. The programs' purpose will determine whether or not the inclusion of hex is necessary.
For example the program I have been working on recently steps through 512 bytes of raw data. The code I wrote is 90% operating on different hex values with regard to their position, value and significance. The 512 bytes if looked at as a whole would be called the MBR(master boot record) of a Windows NTFS volume. The program specifically analyzes bootkits (MBR rootkits) and in the short term allows for reverse engineering and disinfection. In the long run it is merely a piece of my master suite of software I have been working on since I was your age.

Although all numbers can be displayed in whatever base form you would like the relationship between hex and binary is more relevant than hex and decimal(base10.) If you are debugging a program you are generally going to need to be able to reference the memory address space your program is using. This is shown in hex. If you ever want to be able to find a flaw in the new ios you will have to understand how to reverse engineer it. This will leave you with a shit load of lines of assembly code with operators on hexadecimal values representing memory space and the processor register. If you ever hope to write a buffer overflow it is an exercise of pushing more data in a space of memory than the program is designed to handle in certain strategic places. It is necessary to write a program to find these strategic points, and to find out what opportunities these strategic points in turn offer you the ability to do. You will certainly be manipulating hexadecimal values if you ever get this far down the road.

Um yeah. Clearly I did not think my post through before I posted it. My excuse is that I posted it hastily, in my van, in the parking lot of a company I was borrowing internet from. When I read your post I could see code from memory that had hex in it. This very forum uses hex in the colors! Each place holder or digit represents a nybble or 4 bits or half a byte and it IS very easy to wrap your head around what the numbers are doing in hex. Truly decimal is more difficult to deal with and (BCD)binary coded decimal used in digital circuitry is a waste of resources- though necessary sometimes when displaying numbers to peoplemeat.
Yikes! I just betrayed my desire to be incorporeal!