base2base(Nstart,bs​tart,bend)

버전 1.0.0.0 (7.31 KB) 작성자: John D'Errico
Numeric base conversion between any two numeric bases, for any size integer.
다운로드 수: 435
업데이트 날짜: 2016/8/27

라이선스 보기

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

인용 양식

John D'Errico (2024). base2base(Nstart,bstart,bend) (https://www.mathworks.com/matlabcentral/fileexchange/58850-base2base-nstart-bstart-bend), MATLAB Central File Exchange. 검색 날짜: .

MATLAB 릴리스 호환 정보
개발 환경: R2016a
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
버전 게시됨 릴리스 정보
1.0.0.0

Repaired test.