Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Last active February 22, 2021 16:16
Show Gist options
  • Save slightfoot/12b85c91a45faab9aa173b9596bf73c4 to your computer and use it in GitHub Desktop.
Save slightfoot/12b85c91a45faab9aa173b9596bf73c4 to your computer and use it in GitHub Desktop.
Functions that convert ascii text strings of integers and floats/doubles to binary values computers use - by Simon Lightfoot - 22/01/2021
// MIT License
//
// Copyright (c) 2021 Simon Lightfoot
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import 'dart:typed_data';
void main() {
final i = atoi('12345678');
final v = atof('12345678.12345678');
print(i);
print(v);
final b = Uint8List(8);
final d = ByteData.view(b.buffer);
d.setFloat64(0, v);
print(b);
}
/// ascii to integer
int atoi(String value) {
int result = 0;
final s = value.codeUnits;
int index = 0;
int sign = 1;
if(s[index] == 0x2B || s[index] == 0x2D){ // '+' or '-'
if (s[index] == 0x2D){ // '-'
sign = -1;
}
index++;
}
// while (character is > '0' and '9' )
while (index < s.length && s[index] >= 0x30 && s[index] <= 0x39)
{
result = result * 10 + (s[index++] - 0x30); // '0'
}
return result * sign;
}
/// ascii to float
double atof(String value)
{
double integer = 0, sign = 1, numerator = 0, denominator = 1;
final s = value.codeUnits;
int index = 0;
if (s[index] == 0x2B || s[index] == 0x2D){ // '+' or '-'
if (s[index] == 0x2D){ // '-'
sign = -1;
}
index++;
}
// while (character is > '0' and '9' )
while (index < s.length && s[index] >= 0x30 && s[index] <= 0x39){
integer = integer * 10 + (s[index++] - 0x30); // '0'
}
if (index < s.length && s[index] == 0x2E){ // '.'
index++; // skip the dot
// while (character is > '0' and '9' )
while (index < s.length && s[index] >= 0x30 && s[index] <= 0x39){
numerator = numerator * 10 + (s[index++] - 0x30); // '0'
denominator *= 10;
}
}
return (integer + (numerator / denominator)) * sign;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment