Mega Code Archive

 
Categories / Delphi / Examples
 

Numberbases

CODE HERE TO COVERT FROM STRING TO HEX, WHICH COULD BE USED AS A BASIS FOR DOING OTHER NUMBER-BASE CONVERSIONS... function HexStrToNumber(byteString: String): Integer; function GetHexUnits(byteChar: Char): Integer; function GetHexSixteens(byteChar: Char): Integer; function StrToHex(byteString: String); function StrToHex(byteString: String); var total: Integer; begin total := HexStrToNumber(byteString); end; function TParseForm.HexStrToNumber(byteString: String): Integer; {at this point we have one or two characters in a string representing a Hex value... the rightmost character represents units, from 0 to 15, while the leftmost character (if there ARE two) represents sixteens, from 16 to 255, so, taking this into account we do a bit of simple arithmetic...} var units, sixteens, total: Integer; strLen: Integer; begin strLen := Length(byteString); if (strLen = 1) then begin total := GetHexUnits(byteString[1]); Result := total; end; if (strLen = 2) then begin units := GetHexUnits(byteString[2]); sixteens := GetHexSixteens(byteString[1]); total := units + sixteens; Result := total; end; end; function TParseForm.GetHexUnits(byteChar: Char): Integer; var units: Integer; begin units := 0; if (byteChar = '0') then units := 0; if (byteChar = '1') then units := 1; if (byteChar = '2') then units := 2; if (byteChar = '3') then units := 3; if (byteChar = '4') then units := 4; if (byteChar = '5') then units := 5; if (byteChar = '6') then units := 6; if (byteChar = '7') then units := 7; if (byteChar = '8') then units := 8; if (byteChar = '9') then units := 9; if (byteChar = 'A') then units := 10; if (byteChar = 'B') then units := 11; if (byteChar = 'C') then units := 12; if (byteChar = 'D') then units := 13; if (byteChar = 'E') then units := 14; if (byteChar = 'F') then units := 15; Result := units; end; function TParseForm.GetHexSixteens(byteChar: Char): Integer; var sixteens: Integer; begin sixteens := 0; if (byteChar = '0') then sixteens := 0; if (byteChar = '1') then sixteens := 16; if (byteChar = '2') then sixteens := 32; if (byteChar = '3') then sixteens := 48; if (byteChar = '4') then sixteens := 64; if (byteChar = '5') then sixteens := 80; if (byteChar = '6') then sixteens := 96; if (byteChar = '7') then sixteens := 112; if (byteChar = '8') then sixteens := 128; if (byteChar = '9') then sixteens := 144; if (byteChar = 'A') then sixteens := 160; if (byteChar = 'B') then sixteens := 176; if (byteChar = 'C') then sixteens := 192; if (byteChar = 'D') then sixteens := 208; if (byteChar = 'E') then sixteens := 224; if (byteChar = 'F') then sixteens := 240; Result := sixteens; end;