c# 将图像转为字节数组,然后以 0x 的格式转为十六进制?

问题描述 投票:0回答:1

嘿,我正在尝试将我的图像文件转换为十六进制,就像这个网站所做的那样。

但是,当我使用此代码时:

private void btnImg2ByteArray_Click(object sender, EventArgs e)
    {
        OpenFileDialog dialog = new OpenFileDialog();

        dialog.Filter = "Select Valid Image(*.png)|*.png";
        dialog.Multiselect = true;

        if (dialog.ShowDialog() == DialogResult.OK)
        {
            foreach (string fileName in dialog.FileNames)
            {
                using (var ms = new MemoryStream())
                {
                    Image imageIn = new Bitmap(fileName);
                    imageIn.Save(ms, imageIn.RawFormat); //System.Drawing.Imaging.ImageFormat.Png
                    byte[] b = ms.ToArray();
                    txtImage2byte.Text += "[" + Path.GetFileName(fileName) + "]" + Environment.NewLine;
                    StringBuilder hex = new StringBuilder(b.Length * 2);

                    foreach (byte _b in b)
                    {
                        hex.AppendFormat("{0:x2}", _b);
                    }

                    txtImage2byte.Text += hex.ToString();
                }
            }
        }
    }

我得到这样的输出:

89504e470d0a1a0a0000000d49484452000000180000001e0806000000362edee50000000467414d410000b18f0bfc61050000000970485973000001b1000001b1016198280e0000044949444154484bb5564928757d18bfc810ca3c44a6b0a124436241e6154916a605998a9524c5c2860529a16c88905bc6125d3296a94b1642126183cc3391e1f9eeeff9eeb9ef75cee17debfdbe5f3ddde73ee339ff67387f8506761a52999b9b93e6f73f216d2c9586ec1419191914161646b5b5b534393949d3d3d37f458881589ae06a0d2915bebebed4d8d848696969747d7d4d7f0bc4402cc4446c85979717f5f5f5d1d5d595d6448a8787073a3838a08d8d0d26f0907d07c4424cc4d62510e3f3f393363737a9a8a888fcfcfcc8d1d191dcdcdc98c043061d6c602bc66f1334343490b7b737858787535353132d2d2dd1cece0e1378c8a0830d6cc5f831417e7e3e595b5b534b4b0bbdbdbdd1fcfc3c959797537c7c3c0d0d0d69ad883e3e3ed806b6f0d1c7b709d001464646343e3e4e27272794949444c6c6c6fca4292929b4babaaab5fc05d8c207be026413a07830ece9e9a1adad2dee029cf5fafa3aeb7f027ce08b18806c82ecec6c4a4848a0d7d7578a8e8ea6a8a828babbbb63ddfbfb3b9d9e9e32811703c7055fc40024096e6f6f7902552a154d4d4d91b3b3330f0d80b6abababe3a282c0cbb5357c1103b12409e6e6e6c8d0d0909e9e9e7850525353d9092d585050c0eda9542a99c043266e4ff82206624912c011fd0d04060652757535f3171717e4e1e1c1fa95951526f09041270662402f49d0dbdb4b2e2e2e6c54515141cbcbcbcca393209f9898a091911126f090412706e4ddddddd2040b0b0b646262c2c5d207e6202626866263633920083c64d0e903be8881b69524383a3ae2552bd7e77b7b7b141919a95bc7e0211303bed02396240180e2e6e5e5c9ee16b4aeb0ecc08b011ff8c6c5c5f17fd9046ab59a5f1185fc0e586e380231e003df999919fe2f9b004f919999c9853a3c3c6499009cefd8d818ef9ddcdc5cadf45fc0163ec9c9c9bab7ff92a0b9b999eeefef59f1f8f8c83b27202080464747d9018353595949666666e4efef4fc7c7c76c0b1d6c609b9898483737372c07be2440ef666565e9bae2ecec8c0a0b0b79a08a8b8b39a1a9a9294f298ee2f9f999161717a9a4a484271e9f5df8e8e34b024ca5bbbb3be5e4e47c69d3b6b636323030d0758f959515f9f8f8f05963b1858686d2ececaca4b501490db077e08837c1821b1c1c240707070a0909215b5b5beefbd2d2521ec2f6f676d936d5872401b0bbbb4baeaeae1cd8c2c282cacacab8e73158e7e7e76cf3a7904d80a22198a7a7270d0c0c505555157f6c6a6a6a78d2b7b7b7d90eeb1adb143543615f5e5ee8f2f29275026413c009678de3e9e8e8202727270a0e0ee6fa585a5a92bdbd3ddbedefeff3b6c5dca06e586ee9e9e9ac132049203c119e1e329c3b362a64389eaeae2e4e0eacadad918d8d0df5f7f7f3a71475c1af00c4d225105fbcf074adadad5c74bcba00dcda840438aaa0a0209e5a141f1ffe888808d6492e5e1a2835a4feddd5b1bebe9e1380c7970b9d84c9eeececa4e1e1616e69e884ab23aea3980f24f81f2fbf0abb7f009773da6a104473a90000000049454e44ae426082

但是我需要/希望它的格式如下:

0x00、0x00、0x00、0x3f、0xff、0xfc、0x5f、0xff、0xfa、0x7f、0xff、 0xfe、0x7f、0x00、0xfe、0x7e、0x7e、0x7e、0x79、0xcf、0x9e、0x7b、 0x0f、0xde、0x76、0x7f、0xee、0x64、0xff、0xe6、0x6d、0xe7、0xf6、 0x69、0x99、0xf6、0x69、0xbd、0xf6、0x6b、0xbd、0xf6、0x6f、0x99、 0xf6、0x6f、0xc3、0xf6、0x67、0xff、0xe6、0x77、0xff、0xee、0x73、 0xf3、0xce、0x79、0xc7、0x9e、0x1c、0x0e、0x3e、0x0f、0x00、0xfe、 0x66、0x07、0xfe、0x70、0x3f、0xfe、0x72、0x7f、0xfe、0x76、0x0d、 0x5e、0x70、0xed、0x1e、0x5f、0xed、0x5a、0x3f、0xef、0xfc、0x00、 0x00, 0x00

不确定我做错了什么?

c# arrays hex
1个回答
0
投票

要将字节格式化为十六进制,您应该在字节之前放置“0x”,在字节之后放置逗号,所有这些都在字符串内。代码如下所示:

hex.AppendFormat("0x{0:x2}, ", _b);

这会将字节 _b 添加到十六进制格式的十六进制字符串中,前面是“0x”,后面是逗号。

© www.soinside.com 2019 - 2024. All rights reserved.