用于从字符串中解码二进制编码的十进制 (BCD) 的 Java 代码或 lib

发布于 2024-07-12 11:40:31 字数 177 浏览 5 评论 0原文

我有一个由 1 ('\u0031') 和 0('\u0030') 组成的字符串,表示 BCD 值。

具体来说,该字符串由 112 个字符组成 1 和 0,我需要一次提取其中 8 个或 16 个字符,并将它们从 BCD 解码为十进制。

有想法吗? 包裹? 库? 代码? 一切都受到欢迎。

I have a string consisting of 1's ('\u0031') and 0's('\u0030') that represents a BCD value.

Specifically, the string is 112 characters worth of 1's and 0's and I need to extract either 8 or 16 of these at a time and decode them from BCD to decimal.

Ideas? Packages? Libs? Code? All is welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栀梦 2024-07-19 11:40:31

一次提取 4 个字符并使用 Integer.parseInt(string, 2) 应该给出每个数字。 根据您认为合适的方式组合数字。

Extracting 4 characters at a time and use Integer.parseInt(string, 2) should give each digit. Combine the digits as you see fit.

不羁少年 2024-07-19 11:40:31

我认为您错过了所有的乐趣:

这是 Pete Kirkham 建议的基本实现。

花了大约5分钟。

import java.util.List;
import java.util.ArrayList;

public class Binary { 

        public static void main( String [] args ) { 

            for ( int i : Binary.fromString("0000000100100011010001010110011110001001") ) {
                System.out.print( i );      
             }  
             System.out.println();
        }

        public static List<Integer> fromString( String binaryString ) { 

            List<Integer> list   = new ArrayList<Integer>();
            StringBuilder buffer = new StringBuilder();
            int count            = 0;


            for ( char c : binaryString.toCharArray() ) {
                buffer.append( c );
                count++;

                if ( count >= 4 ) { 
                    list.add( Integer.parseInt( buffer.toString(), 2 ) );
                    count = 0;
                    buffer.delete( 0 , 4 );
                }
            }

            return list;
       }
}

I think you're missing all the fun:

Here's a basic implementation of what Pete Kirkham suggested.

Took about 5 mins.

import java.util.List;
import java.util.ArrayList;

public class Binary { 

        public static void main( String [] args ) { 

            for ( int i : Binary.fromString("0000000100100011010001010110011110001001") ) {
                System.out.print( i );      
             }  
             System.out.println();
        }

        public static List<Integer> fromString( String binaryString ) { 

            List<Integer> list   = new ArrayList<Integer>();
            StringBuilder buffer = new StringBuilder();
            int count            = 0;


            for ( char c : binaryString.toCharArray() ) {
                buffer.append( c );
                count++;

                if ( count >= 4 ) { 
                    list.add( Integer.parseInt( buffer.toString(), 2 ) );
                    count = 0;
                    buffer.delete( 0 , 4 );
                }
            }

            return list;
       }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文