BrokenCodes

Jan 30

The last week

Had to implement a magic squares algorithm for my Alog class.

Totally forgot that “x^y” was not x to the y power. However it was inside the truth condition of a for loop so I didn’t get an error and sat their for twenty minutes wondering why my algo ran forever or not at all. Finally fixed it and felt stupid. That being said the grad student didn’t notice it either so I guess I don’t feel as bad.

Got to play with OpenGL in my Comp Arc Class. That was pretty fun. I had always been curious how OGL was implemented, but never had the time to check it out myself. Neat little tutorial for it called JPot, explains the basics and gives you a good overveiw of how it’s implemented in C++, lets you play around with the code then re-compiles it so you can see the changes.


Jan 15

Getting the Hex value of a Floating Point in Java

So in my computer Architecture lab today our teacher decided to try and make us hate Java.

First we had to convert an integer to it’s hexadecimal representation. Easy as evidenced below.

String is for the most part just as easy, it just takes a loop to read through each char in the string and return the Hex value of each char.

Floating Point to hex on the other hand, nope, not these are not the hex digits you are looking for.

First, lets look at the wrong way to do it. We will print out the input and the output it converts too. For this we will use the “Float.toHexString(float)” method in the Java API.

public static void wrongFloatToHex(float floatInput){

System.out.println(“Float ” + floatInput + ” in Hex: “
+ Float.toHexString(floatInput));

}

We will test it with these four calls

wrongFloatHex(-1);
wrongFloatHex(0);
wrongFloatHex(1);
wrongFloatHex(2);

Giving us this output.

Float -1.0 in Hex: -0x1.0p0
Float 0.0 in Hex: 0x0.0p0
Float 1.0 in Hex: 0x1.0p0
Float 2.0 in Hex: 0x1.0p1

While technically written in hex, this isn’t what we want. At all. We want the actual Hex representing the binary in memory.

We have an issue here. Integer and Char(We look at each individual char in the String), do give us the correct answer when we use their respective “toHexString()” methods. We are now dealing with the fact that Java has Type Safety, and for some reason beyond my understanding, least until class next Wednesday, java won’t let me see the Hex representing  that floating point number.

Now, how do I overcome this without going and using C/C++? Easy! We write to a binary file. Here is my code.

public static void floatHex(float floatInput){

String strFilePath = “C:\float.bin”;
try {

//This is for writing your float to the binary file. You need to create a file at the location.

FileOutputStream fos = new FileOutputStream(strFilePath);

DataOutputStream dos = new DataOutputStream(fos);

dos.writeFloat(floatInput);
dos.close();
fos.close();

// Now we read the binary file back out.

FileInputStream fin = new FileInputStream(strFilePath);
DataInputStream din = new DataInputStream(fin);

int temp2 = din.readInt();

din.close();
fin.close();

System.out.println(“Float ” + floatInput + ” in Hex: “+ Integer.toHexString(temp2));

{

//Way to lazy to format these exceptions

catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

Now with these inputs

floatHex(-1);
floatHex(0);
floatHex(1);
floatHex(2);

We should get these outputs

Float -1.0 in Hex: bf800000
Float 0.0 in Hex: 0
Float 1.0 in Hex: 3f800000
Float 2.0 in Hex: 40000000

Also, I used data file because it writes to files in binary.


Jan 13

So yeah, Hi.

Hi I’m a Senior Computer Science Major, I’ve got two semester long projects and a bunch of small personal projects for my spare time. I’ll be posting lines, snippits, bits, scripts, problems, and solutions. These will frequently be accompanied by links to repositories and other things.

All original work is posted under the a Creative Commons Attribution-Share Alike 3.0 United States License, unless otherwise noted.

Creative Commons License


Page 1 of 1
Creative Commons License
Unless otherwise noted all posts featuring my original work are licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.