base2base(Nstart,bstart,bend)
Existing functions in MATLAB like dec2bin, dec2base, base2dec are limited in scope to work in double precision. So numbers cannot exceed 2^53-1. As well, those tools cannot convert to and from bases higher than 36, due to the character encoding employed. Yes, hex is a nice way to read hexadecimal numbers. But things can get nasty when working in say base 36, because 'O' and '0' can be difficult to distinguish. Finally, the existing tools cannot convert directly between two bases. So if you wanted to move between base 2 and base 3, you might use base2dec, then dec2base. But that forces you to use double precision as an intermediate storage. So if your number exceeded 2^53-1, that scheme would fail.
base2base resolves all of those issues, by providing a tool with no limits. If character inputs are provided, it will return a character encoded output, just as the built-in MATLAB tools do. A few examples should show the various problems base2base can solve.
Example: Convert decimal numbers 0:10 from base 2 into base 3
Nstart = dec2bin(0:10)
Nstart =
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
Nend = base2base(Nstart,2,3)
Nend =
000
001
002
010
011
012
020
021
022
100
101
Example: base conversion for a really large number (this conversion was verifed using vpi2base)
base2base('1234567890123456789012345678901234567890123456789012345678901234567890',10,8)
ans =
26712730460557502321115663104163350570542770265664630627263177431331617605322
Example: hex to binary
base2base('FFFBC1234A',16,2)
ans =
1111111111111011110000010010001101001010
The above examples all worked using character encoded digits. But if we want to go past base 36, that gets more difficult in an encoded form.
Example: conversion from base 10 to base 1000
base2base([1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9],10,1000)
ans =
123 456 789 123 456 789
Example: conversion between two bases, both of which are larger than 36. I've chosen number bases where the result will be easy to verify here.
base2base([233 455 876 342 645],1000,100)
ans =
2 33 45 58 76 34 26 45
Citar como
John D'Errico (2024). base2base(Nstart,bstart,bend) (https://www.mathworks.com/matlabcentral/fileexchange/58850-base2base-nstart-bstart-bend), MATLAB Central File Exchange. Recuperado .
Compatibilidad con la versión de MATLAB
Compatibilidad con las plataformas
Windows macOS LinuxCategorías
Etiquetas
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Descubra Live Editor
Cree scripts con código, salida y texto formateado en un documento ejecutable.
Versión | Publicado | Notas de la versión | |
---|---|---|---|
1.0.0.0 | Repaired test. |