{"id":26343966,"url":"https://github.com/iplaylf2/lf2bitconverter","last_synced_at":"2025-08-30T06:36:56.826Z","repository":{"id":178597117,"uuid":"152108831","full_name":"iplaylf2/LF2BitConverter","owner":"iplaylf2","description":"BitConverter","archived":false,"fork":false,"pushed_at":"2018-10-31T09:53:49.000Z","size":58,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-16T05:19:49.485Z","etag":null,"topics":["c-sharp","dotnet-standard","expression-tree"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iplaylf2.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2018-10-08T16:06:08.000Z","updated_at":"2021-09-20T20:10:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"24768559-b60f-45ee-bb54-4c44a74f3556","html_url":"https://github.com/iplaylf2/LF2BitConverter","commit_stats":null,"previous_names":["iplaylf2/lf2bitconverter"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/iplaylf2/LF2BitConverter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iplaylf2%2FLF2BitConverter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iplaylf2%2FLF2BitConverter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iplaylf2%2FLF2BitConverter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iplaylf2%2FLF2BitConverter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iplaylf2","download_url":"https://codeload.github.com/iplaylf2/LF2BitConverter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iplaylf2%2FLF2BitConverter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272815821,"owners_count":24997661,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-30T02:00:09.474Z","response_time":77,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c-sharp","dotnet-standard","expression-tree"],"created_at":"2025-03-16T05:19:48.122Z","updated_at":"2025-08-30T06:36:56.816Z","avatar_url":"https://github.com/iplaylf2.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LF2BitConverter\nSystem.BitConverter的扩展版，使用同样的编码格式。用于自定义对象与Byte[]互转。   \n\n## 用法\n\n接口简单，支持大小端转换。   \n\n``` C#\n//Convert字段顺序为类型定义时的顺序\n//转换的类型也是定义时的类型\nclass Foo\n{\n    public Int32 A;\n    public Int32 B;\n}\n\nvoid Demo()\n{\n    Foo foo = new Foo { A = 1, B = 2 };\n\n    Byte[] littleEndian_bytes = BitConverterEX.LittleEndian.GetBytes(foo);  //new Byte[]{1,0,0,0,2,0,0,0}\n    Byte[] bigEndian_bytes = BitConverterEX.BigEndian.GetBytes(foo);  //new Byte[]{0,0,0,1,0,0,0,2}\n\n    Int32 startIndex = 0;\n    Foo new_foo_1 = BitConverterEX.LittleEndian.ToObject\u003cFoo\u003e(littleEndian_bytes, ref startIndex);  //{A=1,B=2}\n\n    startIndex = 0;\n    Foo new_foo_2 = BitConverterEX.BigEndian.ToObject\u003cFoo\u003e(bigEndian_bytes, ref startIndex);    //{A=1,B=2}\n}\n```\n\n默认提供转换方法的类型为System.BitConverter支持的类型,以及Byte。    \n\n``` C#\nclass Foo\n{\n    public Double A;\n}\n```\n\n支持数组转换。    \n\n``` C#\nclass Foo\n{\n    /// \u003csummary\u003e\n    /// 按元素个数计数，固定长度为10的数组\n    /// \u003c/summary\u003e\n    [ConvertArray(CountBy.Item, Length = 10)]\n    public Int32 FooArray;\n\n    /// \u003csummary\u003e\n    /// 存放BarArray getbytes后的字节数\n    /// \u003c/summary\u003e\n    public Int32 BarCount;\n\n    /// \u003csummary\u003e\n    /// 按getbytes后字节长度计数，不定长度的数组\n    /// \u003c/summary\u003e\n    [ConvertArray(CountBy.Byte, LengthFrom = nameof(BarCount))]\n    public Char[] BarArray;\n}\n```\n\n支持嵌套类型。     \n\n``` C#\nclass Foo\n{\n    public Int32 A;\n    public Double B;\n}\n\nclass Bar\n{\n    /// \u003csummary\u003e\n    /// 甚至支持嵌套类型的数组。\n    /// \u003c/summary\u003e\n    [ConvertArray(CountBy.Item, Length = 10)]\n    public Foo[] FooArray;    \n}\n```\n\n## 扩展\n\n扩展方便，派生ConvertMemberAttribute后重写公共方法实现扩展。\n\n``` C#\n//派生ConvertAsAttribute，指定成员数值在转换时的类型。\nclass ConvertAsAttribute : ConvertMemberAttribute\n{\n    public ConvertAsAttribute(Type type)\n    {\n        ConvertType = type;\n    }\n    \n    public override Type OnGetConvertType(Type lastResult)\n    {\n        return ConvertType;\n    }\n\n    private readonly Type ConvertType;\n}\n\nenum Foo\n{\n    A,B,C\n}\n\nclass Bar\n{\n    //枚举类型在转换时会被当作是Byte类型\n    [ConvertAs(typeof(Byte))]\n    public Foo Foo;\n}\n```\n\n本库内置的转换数组ConvertArrayAttribute，转换字符串ConvertStringAttribute等皆通过派生ConvertMemberAttribute实现。\n\n## 说明\n使用表达式树动态构造转换函数，性能与手写转换代码相当。   \n\n更多demo在demo目录。    ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiplaylf2%2Flf2bitconverter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiplaylf2%2Flf2bitconverter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiplaylf2%2Flf2bitconverter/lists"}