简体   繁体   中英

Byte[]String to String in C#

I have the byte Array String like this that it's encoded by Base64 method on android like this one

return Base64.encodeToString(b, Base64.URL_SAFE);

so I've sened it to my web api an I've Got Something like This (this is my byte array)

iVBORw0KGgoAAAANSUhEUgAAAgAAAAGACAIAAABUQk3oAAAAA3NCSVQICAjb4U_gAAAgAElEQVR4 nOy925IkSZIddo6aR9ZcdoWEQIR4Af__Wyh4Iz-AT5AVPhAQrIC705VhevigFzOPiKrO7unZ7axx nZ6sCA93c3NzM70cvRj_6f_9vwEAhqT-AECAzh--T9ouJkSAeTm3FsTzXb5Fnlc_t_26nw7unSQU p3U3XtJqR5D4gcfUywYfjwg6H3u6JNv5yFA808urPviavj8gF30m-h__7Z_-r__r__yn__pf39_f ..................................... PRqEJ9oLFmsrpX1vhBXq82y1knRVUKFRwRwDgy8EiWTGfD5ZvpEz2dB54fIv8sgaY1oHz-e0M_l0 Fa1ym728AKyom8xstoaVBhv0YolD1jkAInXMwVi9JEXcfUqgHTSZz7oIYwzjGHYjRiXJOX2XLDkE H1uv5MqlbdqjV4KD20eaW6rv4-HT0ePbQicqOzC7_wsSX-sS9mzikrwvurHr_rux9-h8ijOejjxc uwmAl8bX6wf5G_HTj7D7j9z6I4Lkor9Tan-mmQGTUWYgImFgoEnOrPsQVzQgJCnD8yFxAyw21d2E SB5TJNFyNSAA1HbuJmNq9ff_tbF3Vth9OwLye3YKpT12S53VfH5wVCSP3JvZcGvq8

also I've use an other method on the client side that it responed me like this one

JFIF C C " } !1AQa"q2 #B R $3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w !1AQaq"2 B #3R br $4 % &'() 56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz W5 K4( ڇ R+$Sk W yd yŬ, oR #- 9, ӗb'Nx sJ< Jo ZsM i a 'MΪ ֪) q j : $[ 2 dqPp z <? $+ a rMz|֝t b \\ C $.I Q Q Q 3 S k ;w ɽ 6 <7 թR Qvs * z + ngtӊ,| < }ON =O \\ 3 Y4 q ipy m $ 29 q y9䁀x = 䍣>) O_6 d:,fISq!_ϕQ ' $ k IW ;/ek N 륚 e E \\% %yUR uի S K [ 4~`f;\\ ݹ * Ǯy k> 4

which is correct ?

how do i convert it to the byte[]?

I've find these two method one work but the file is damaged and an other one got an exeption

method 1 :

 static byte[] GetBytes(string str)
    {
        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }

method 2 (exeption):

public static byte[] StringToByteArray(string hex)
    {
        return Enumerable.Range(0, hex.Length)
                         .Where(x => x % 2 == 0)
                         .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                         .ToArray();
    }

so what Shall i do ??

在C#中,您可以: Byte[] bytes = Convert.FromBase64String(s);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM