Skip to content

Commit

Permalink
Add non-editable bin/hex fields
Browse files Browse the repository at this point in the history
  • Loading branch information
afterdusk committed Apr 24, 2021
1 parent cb036da commit a1a5f09
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const APP_TITLE = "IEEE 754-Style Floating-Point Converter";
export const POSITIVE_INFINITY_STRING = "infinity";
export const NEGATIVE_INFINITY_STRING = "-infinity";
export const NAN_STRING = "NaN";
export const HEX_PREFIX_STRING = "0x";

// bignumber.js
export const BIGNUMBER_DECIMAL_PLACES = 300;
Expand Down
14 changes: 14 additions & 0 deletions src/Flop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,20 @@ export const stringifyBits = (bits: boolean[]): string => {
return bits.map((e) => (e ? "1" : "0")).join("");
};

/**
* Converts bit array to hexadecimal string.
* @param bits boolean array representing bits (length should be in multiple of 4)
* @returns hexadecimal string
*/
export const stringifyBitsToHex = (bits: boolean[]): string => {
return Array.from(Array(Math.floor(bits.length / 4)).keys())
.map((i) => i * 4)
.map((i) => bits.slice(i, i + 4))
.map((e) => stringifyBits(e))
.map((e) => parseInt(e, 2).toString(16))
.join("");
};

/**
* Converts binary string to bit array.
* @param bitString binary string
Expand Down
6 changes: 6 additions & 0 deletions src/components/Format.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ const Format: FC<FormatProps> = (props: FormatProps): ReactElement => {
clearInput={flop === null}
stored={Flop.stringifyFlop(storedFlop)}
error={error ? Flop.stringifyFlop(error) : ""}
binaryRepresentation={Flop.stringifyBits(
[sign, exponent, significand].flat(1)
)}
hexRepresentation={Flop.stringifyBitsToHex(
[sign, exponent, significand].flat(1)
)}
updateValue={(inputValue: string) =>
onFlopUpdate(Flop.generateFlop(inputValue))
}
Expand Down
22 changes: 22 additions & 0 deletions src/components/Panel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { FC, useState, ReactElement, useEffect } from "react";
import styled from "styled-components";
import * as Constants from "../Constants";

const Wrapper = styled.div`
box-sizing: border-box;
Expand Down Expand Up @@ -32,6 +33,8 @@ type PanelProps = {
clearInput: boolean;
stored: string;
error: string;
binaryRepresentation: string;
hexRepresentation: string;
updateValue: (inputValue: string) => void;
};

Expand Down Expand Up @@ -83,6 +86,25 @@ const Panel: FC<PanelProps> = (props: PanelProps): ReactElement => {
<InputField disabled readOnly value={props.error} />
</Col>
</Row>
{/* Binary Representation */}
<Row>
<Col size={2}>
<FieldName>Binary Representation</FieldName>
</Col>
<Col size={5}>
<InputField disabled readOnly value={props.binaryRepresentation} />
</Col>
</Row>
{/* Hexadecimal Representation */}
<Row>
<Col size={2}>
<FieldName>Hex Representation</FieldName>
</Col>
<Col size={5}>
{Constants.HEX_PREFIX_STRING}
<InputField disabled readOnly value={props.hexRepresentation} />
</Col>
</Row>
</Wrapper>
);
};
Expand Down

0 comments on commit a1a5f09

Please sign in to comment.