Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8509735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:30:57+00:00 2026-06-11T03:30:57+00:00

Redis keys are binary safe . I’d like to mess around and put binary

  • 0

Redis keys are binary safe. I’d like to mess around and put binary into redis using C#. My client of choice doesn’t support writing binary keys it uses keys and it make sense. However i am just fooling around so tell me how i can do this.

How do i convert a raw byte[] into a string? At first i was thinking about converting a byte[] to a utf8 string however unicode has some checks to see if its valid or not. So raw binary should fail.

Actually i tried it out. Instead of failing i got a strange result. My main question is how do i convert a raw byte[] to the equivalent string? As in have the raw byte[] as a string and not encoding as base32/64/hex/whatever. My unimportant question is why did i get a 512 byte string instead of an exception saying this is not a valid UTF8 string?

code

var rainbow = new byte[256];
for (int i = 0; i < 256; i++)
{
    rainbow[i] = (byte)i;
}
var sz = Encoding.UTF8.GetString(rainbow);
var szarr = Encoding.UTF8.GetBytes(sz);
Console.WriteLine("{0} {1} {2}", ByteArraysEqual(szarr, rainbow), szarr.Length, rainbow.Length);

Output

False 512 256

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:30:59+00:00Added an answer on June 11, 2026 at 3:30 am

    You have to use some kind of encoding to convert bytes to a string. The encoding iso-8859-1 will give the correct result:

    var sz = Encoding.GetEncoding("iso-8859-1").GetString(rainbow);
    var szarr = Encoding.GetEncoding("iso-8859-1").GetBytes(sz);
    Console.WriteLine("{0} {1} {2}", ByteArraysEqual(szarr, rainbow), szarr.Length, rainbow.Length);
    

    True 256 256

    The thing is that UTF8 requires more than one bytes per character. It can encode the first 128 characters with one byte:

    Console.Write(Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(new byte[] { 127 })).Length);
    

    1

    But the rest require three bytes:

    Console.Write(Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(new byte[] { 128 })).Length);
    

    3

    So, when you convert bytes 0-255 to a string and back with UTF8, the first 128 come back as one byte, but the last 128 come back as 3. 128 + 3*128 = 512, hence your result.

    ASCII doesn’t know what to do with bytes past 128, so they just get encoded as ?, and come back as one byte also.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using redis-rb in a Rails app, the following doesn't work: irb> keys = $redis.keys(autocomplete*)
I am using redis and node (with node_redis ) and I would like to
Using redis-rb, how can I push a hash into a list? Do I have
I am using Redis, and am storing many keys with no TTL (I need
Is it possible to persist only certain keys to disk using Redis? Is the
Is there a way to print the number of keys in Redis? I am
Using redis hgetall , gets all items as a dict. How do i get
Using the Redis info command, I am able to get all the stats of
I'm integrating redis into my NodeJS server app, and I'm trying to figure out
I'm new to redis, and I'm trying out various examples. However, it doesn't seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.